Exclude Certain Categories And Tags From WordPress Search

Want to exclude certain categories and tags from WordPress search? If yes then you have come to the right place for the answer.

When you search for any article on your WordPress site the search is usually conducted on all the categories & tags.

In this article, we will show how you can conveniently certain categories and tags from WordPress search results.

But first, let’s understand why it is important to exclude certain categories and tags from WordPress search.

Why To Exclude Certain Categories And Tags From WordPress Search

Category And Tags In WordPress Site

The default WordPress Search feature shows the results from all WordPress categories & tags.

This is okay for most of the websites out there & it does not affect the websites’ performance in anyway.

In some cases, you may want to exclude some posts in certain categories and tags from the WordPress search, for example, password protected posts or posts that are not part of your normal WordPress site content.

You can assign these posts to the category or tag & then exclude the category or tag from your blog to stop them from displaying on the blog.

In this manner you can keep organize your WordPress site in a proper manner.

Exclude Certain Categories And Tags From WordPress Search

Exclude Certain Categories And Tags From WordPress Search

To exclude a specific category or various categories, a specific tag or various tags from WordPress Search, you will have to add a piece of code to your WordPress site.

Exclude Category Or Categories

Find the Category ID of the Specific Category, that you want to exclude from the WordPress Search Results.

Then add the following code to your functions.php file & don’t forget to replace ‘9’ with the Category ID of the Specific Category that you want to exclude from the search results.

function wp_search_filter($query)
{
 if($query->is_search && !is_admin())
 $query->set('cat','-9');
 return $query;
}
add_filter('pre_get_posts','wp_search_filter');

If you want to exclude many categories simultaneously then, add the following code to your functions.php file & don’t forget to replace ‘9’, ’10’ & ’11’ with the Category IDs of all the Categories, that you want to exclude from the search results.

function wp_search_filter($query)
{
 if($query->is_search && !is_admin())
 $query->set('cat','-9, -10, -11');
 return $query;
}
add_filter('pre_get_posts','wp_search_filter');

Exclude Tag Or Tags

If you want to exclude all the posts that come under a Specific Tag, then you can use the code given below in you functions.php file. Just don’t forget to replace ‘3’ with the Specific Tag ID of the Tag that you want to exclude.

function wp_search_filter($query)
{
 if($query->is_search && !is_admin())
 $query->set('tag','-3');
 return $query;
 }
add_filter('pre_get_posts','wp_search_filter');

If you want to exclude the posts that come under Various Tags, then you can use the code given below in you functions.php file. Just don’t forget to replace ‘5’, ‘6’, ‘7’ with the Tag IDs of the Tags, that you want to replace.

function wp_search_filter($query)
{
 if($query->is_search && !is_admin())
 $query->set('tag','-5, -6, -7');
 return $query;
}
add_filter('pre_get_posts','wp_search_filter');

Conclusion

Working with WordPress is really easy & delightful if you know your way around it.

We sincerely hope that this article will help you exclude specific categories & tags from your WordPress Search Results & help you achieve the functionality that you want.

If you want to use a plugin to exclude a category you can use Ultimate Category Excluder WordPress plugin.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Copy link
Powered by Social Snap