1

So I am trying to edit my Search so it searches only for specific categories. I am adding this code to my functions.php file in child theme:

function searchcategory($query) {
    if ($query->is_search) {
        $query->set('cat','37');
    }
    return $query;
}
add_filter('pre_get_posts','searchcategory');

So this is the issue: For example, when I run this code, my search does not even find anything.

Important notes: 1) First of all, I have created several taxonomies and custom post types with Toolset plugin, and these is ID of one of those taxonomies - I am looking up its ID by hovering cursor over the name of taxonomy. Could this be an issue? 2) When I run this code, which supposes to exclude all pages from search results, I still see some of the pages. It is weird.

function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('post_type', 'page');
    }
    return $query;
}

add_filter('pre_get_posts','SearchFilter');

Does anyone know what may cause this issue?

  • What if you change '37' to 37, an integer? – Jacob Peattie Jun 13 '18 at 4:11
  • codex.wordpress.org/Plugin_API/Action_Reference/… - what if you remove the line return $query; ? are you targeting a category or one of your taxonomies? – Michael Jun 13 '18 at 4:40
  • I am actually targeting one taxonomy, but I would go with category as well if it worked. So I need either taxonomy or category – tera_789 Jun 13 '18 at 4:59
  • Also, if I remove return $query;, it does not query anything. Just shows no results – tera_789 Jun 13 '18 at 5:06
2

This code should work OK, but... There are some problems with it. Let's break it apart and add some comments.

function searchcategory($query) {
    if ($query->is_search) {
        // Checking only for is_search is very risky. It will be set to true
        // whenever param "s" is set. So your function will modify any wp_query with s,
        // for instance the one in wp-admin... But also any other, even if
        // the query isn't querying for posts...

        $query->set('cat','37');
        // You pass ID of term in here. It's a number. Passing it as string
        // is not a problem, but it's another bad practice, because it will
        // have to be converted to number.
    }
    return $query;

    // It's an action - you don't have to return anything in it. Your result 
    // will be ignored.
}
add_filter('pre_get_posts','searchcategory');

// pre_get_posts is and action and not a filter.
// Due to the way actions/filters are implemented,
// you can assign your callback using `add_filter`,
// but it isn't a good practice.

So how to write it nicer?

function searchcategory($query) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
        $query->set( 'cat', 37 );
    }
}
add_action( 'pre_get_posts', 'searchcategory' );
  • thanks it worked as well. Now I am trying to make Search return different posts when I am on different pages. I am trying to use this code: function search_classesx($query) { if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) { if is_page(54) { $query->set( 'cat', 37 ); } if is_page(1451) { $query->set( 'cat', 40 ); } } } add_action( 'pre_get_posts', 'search_classesx' ); But it does not work. Do you by any chance know why? thanks – tera_789 Jun 14 '18 at 0:14
0

I solved the issue. I added this same code:

function searchcategory($query) {
if ($query->is_search) {
$query->set('cat','37');
}
return $query;
}
add_filter('pre_get_posts','searchcategory');

The problem was with Toolset plugin. For some reason, it was messing up the search results based on categories/taxonomies. Whatever it was, the code above works fine with original WordPress categories.

  • 1
    Read the answer from @Krzysiek. Many improvements can be made on those few lines. – Bjorn Jun 13 '18 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.