• Hello there,

    I am setting up a wordpress page with the classipress template. I have done some modifying of this template, and I am faced with a search issue!

    Here is a link to the site: https://abdulslist.com/classifieds

    I am unable to limit my search to a single category. It will pull up a category view, and display all of the posts within a single category, but when the “s=whatever” is in the URL, it bypasses that category filter!

    Oh what a headache, I am using only one plugin: Breadcrumb NavXT v3.3.0

    Does anyone have any idea of where I should begin troubleshooting?

    Thanks,
    Brian

Viewing 3 replies - 1 through 3 (of 3 total)
  • Can you please Share with us how did you fixed it please

    I’m getting the same exact problem. It’s like one can’t have their cake and eat it too. I have tried editing search.php in a custom theme in various ways, but they all have quirks so far:

    – using in_category(‘cars’) inside a loop causes pagination to break!

    – using query_posts(‘cat=4’) before the loop gives me category filter but ignores the search!

    Hey! I found the fix! If you leave your search.php alone and let it do what it normally does, you can make (or edit) functions.php and tack on something like this:

    $sCategory = 'chickens';
    
    add_filter('pre_get_posts','filterPosts');
    
    function filterPosts(&$args) {
    	if ($args->is_feed) {
    		$cats = get_categories();
    		foreach ($cats as $cat) {
    			if ($cat->name == $sCategory) {
    				$nCatID = $cat->cat_ID;
    				break;
    			}
    		}
    		$args->set('cat',$nCatID);
    	}
    	if ($args->is_search) {
    		$cats = get_categories();
    		foreach ($cats as $cat) {
    			if ($cat->name == $sCategory) {
    				$nCatID = $cat->cat_ID;
    				break;
    			}
    		}
    		$args->set('cat',$nCatID);
    	}
    	return $args;
    }

    The first part referring to is_feed() helped me restrict a feed to a category. The second part referring to is_search() helped me restrict a search to a given category.

    By trying to do this in search.php, instead, you will be hit with all kinds of “can’t get there from here” situations for some reason. Perhaps a bug in WordPress? I don’t know exactly. And the default query that is passed to search.php by default is already established and cannot be hooked for some reason unless you do it from functions.php, which gets called earlier than search.php. You can see this if you add an add_filter(‘all’,’test’) in search.php to dump out results to see what search.php does — it won’t let you have access to the main query it uses by default. This is why the intercept on pre_get_posts must be done from functions.php.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Issues when searching by category’ is closed to new replies.