How to Exclude Categories from Search in WordPress

How to Exclude Categories from Search in WordPress

If you have a varied WordPress blog, you’ll most likely have arranged it carefully so that different sections are kept away from each other. For example, you might not choose to display all of your categories on the homepage. Similarly, you may exclude certain pages or categories from your related posts plug-in. We can even set some categories to “invisible” if they serve some internal purpose.

You might have done something similar with posts and pages with the former acting as a traditional blog, and the latter functioning more like a business website. Whatever your arrangements, structuring information for various categories of users is critical for a smooth experience.

However, the search function on WordPress obliterates all these distinctions. If you use the built-in widget for search on your sidebar, it will pull in every single post or page that matches the chosen term. This risks breaking the segmentation of your site. It might surface some posts or pages that wouldn’t fit what a casual surfer is looking for. Whatever your reason, in this article you’re going to look at how to exclude categories from search in WordPress – and some other tricks as well!

Excluding Categories from Search

If I take my test blog and do a search for a simple term, I get posts from multiple categories as shown here:

exclude categories from search

The screenshot above shows search results from the “politics”, “financial matters”, and “personal” categories. Let’s say for example that I want to exclude the “politics” category from my search results. To implement this kind of functionality, open up your functions.php file and paste the following before the closing ?> PHP tag:

function exclude_category_from_search($query) {
if ($query->is_search) {
$cat_id = get_cat_ID('politics');
$query->set('cat', '-'.$cat_id);
}
return $query;
}
add_filter('pre_get_posts','exclude_category_from_search');

If you don’t know how to add code like this to your WordPress installation, check out my earlier tutorial on the same. This snippet of code hooks into the “pre_get_posts” filter, extracts the category ID from the “politics” slug and excludes it from the search query.

Replace the bolded “politics” with your own category slug. If you already know the category ID, you can execute it directly by changing the fourth line to this:

$query->set('cat', '-24');

Where “24” represents the category ID of “politics”. If you want, you can have a, separated list of category IDs that you want to exclude. For example,

$query->set('cat', '-24,-45,-52');

Don’t forget the minus (-) sign in front of each number!

With the initial snippet of code displayed above, the same search query that I executed earlier brings different results as shown here:

You can see that the “politics” category has been removed entirely and only the remaining ones are displayed.

politics category gone

So if you have “hidden” categories that are not for public consumption for whatever reason, you can use this technique to keep them away from the search page in addition to whatever other measures you already have in place.

Excluding Pages

If you’re maintaining a separation between your blog posts and a regular business website, it doesn’t make sense to have the latter appear in search results. Since you’re most likely using pages for the front-end of your business website, we can simply exclude that category entirely from the search results like this:

function exclude_posts_from_search($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','exclude_posts_from_search');

You can even modify this piece of code to change the number of results displayed on the search page and do a lot of other interesting stuff. You can get a complete list of the various parameters of the query object by checking out the WordPress documentation here.

The search page is where visitors go to find specific content on your site. Don’t mess it up by confusing them with irrelevancies.

2 Comments on “How to Exclude Categories from Search in WordPress”!

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.