A typical WordPress website contains many different types of content. We have the most basic distinction between pages and posts, which serve completely different purposes. We also have different types of blog posts, and some that are meant specifically to be found by our customers, and others for different reasons – like say legal notices.
Not All Content is Targeted Towards Customers
It’s easy to separate the different types of content into categories. We can have one category for informational blog posts, another about personal stories, and so on and so forth. In a well-planned, laser focused website, everything will be relevant to customers in one way or another. But some sites grow organically from different origins.
Perhaps there are certain articles that you don’t want people to see in your site’s search results. You don’t want to remove these posts, because they perhaps still generate traffic, or have inbound links pointing to them, and it doesn’t make sense to lose the link juice.
What I’ll show you in this tutorial, is how to exclude certain categories from showing up in the search results of your website. This way, you can still have the content, but minimize its exposure.
Preventing ONE Category from Appearing in Search
To start with, I’ll assume that you want to exclude just one category from search. It’s an easier method. In the second section, I’ll give you the code to exclude multiple categories.
Let’s say I perform a search on my site, and get the following result – a post with the category “Personal” as shown here:
We’re going to write code to exclude “Personal” posts from the search results.
Getting the Category Slug
Every category has a “slug” and an “ID” associated with it. Whenever convenient, I prefer to work with slugs because they’re easy to remember, and easier to find. In your WordPress dashboard, navigate to the categories section by clicking Posts -> Categories.
Now find the category you want to exclude. The slug will be next to it under the appropriate column as shown here:
Usually, it’s just a lowercase version of the category name – but it doesn’t have to be.
Adding Code
Once you have the slug, paste the following code into WordPress. If you don’t know how to do this, read my earlier tutorial on how to paste snippets into WordPress.
Here’s the code:
function disable_category_search($query) { if ($query->is_search) { $cat_id = get_cat_ID('personal'); $query->set('cat', '-'.$cat_id); } return $query; } add_filter('pre_get_posts','disable_category_search');
Replace ‘personal’ with the slug name you obtained in the previous step, save your changes and you’re done! Here you can see the same query no longer shows the same post because it belongs to the “Personal” category:
Now for multiple categories.
Preventing Multiple Categories from Showing Up
Technically, we can tweak the code above a bit to include more than one category, but it’s a bit clumsy. Instead, we’ll just use category IDs instead of slugs. You can obtain the category ID by hovering your mouse pointer over the “Edit” button in the WordPress category screen like this:
The URL in the status bar will show you the “tag_ID” parameter containing the category ID. In this case, it’s “4”.
So first get the category IDs of all the categories that you want to exclude from search. Then paste the following code into functions.php, or a custom PHP code file:
function disable_category_search($query) { if ($query->is_search) { $cat_id = get_cat_ID('personal'); $query->set('cat', '-3,-4'); } return $query; } add_filter('pre_get_posts','disable_category_search');
Replace “-3,-4” with your category IDs. Note that these are negative numbers, since we’re telling WordPress to exclude them instead of including them. So my category numbers of “3” and “4”, become “-3” and “-4” respectively.
Save your changes, and now your chosen categories will no longer appear in your search results!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Warszawa says
This is what I was looking for. I wanted to exclude 1 category from the search results. There is no such feature inside wordpress itself. Now I know how to deal with it. It looks like adding the code to the file – will it disappear when updating the theme? Will you need to add it every time you update your theme files?
Bhagwad Park says
You have two options:
1. Create a custom plugin for code (recommended): https://www.namehero.com/blog/where-to-put-custom-code-in-wordpress-hint-use-a-plugin/
2. Use a child theme for your functions.php file: https://www.namehero.com/knowledgebase/157/How-To-Create-A-Child-Theme.html
Laurie says
great instructions and so easy thank you 🙂
Dan Miller says
In the second code snippet (preventing multiple categories), you have entered the category ids.
In that case do I need the get_cat_ID line?
$cat_id = get_cat_ID(‘personal’);