How to Exclude Specific Results from WordPress Search
When trying to give your website’s visitors a way to navigate your website, the two elements most worthy of your attention are the navigation menu and the search functionality. Adding the navigation menu can be a breeze. The search bar is just another widget you can easily add. Keep in mind, however, that both require fine-tuning to shine fully.
When it comes to the search bar, your first instinct might be to reach for a WordPress search plugin and use it to improve on the native search functionality. Before you do that, however, you could ensure you’ve exhausted other options you have in WordPress. Excluding search results is one of them.
In this article, we’ll show you:
The primary role of the search function is to produce results based on a user’s query. If you want the feature to be useful and add to the overall user experience, you’ll want the results to be as relevant and zeroed-in as possible.
One of the ways you can improve relevance is by removing any results that are usually irrelevant, such as the home page or the about us page. You can also use the search results exclusion to reduce the clutter. You can allow your visitors to search only several posts and pages, instead of the whole website.
Finally, excluding specific results can help you manage access to the content on your website. If there are posts or pages, you don’t want to be readily found, excluding them from the navigation and the search results would be an excellent way to hide them without actually hiding them.
You can use a plugin to exclude specific posts and pages from your website’s search results. It’s called Search Exclude, and you can find it easily by navigating to Plugins > Add New in your website’s backend and typing its name in the search box. Once you find it, install and activate it.
Once activated, you’ll notice that your existing posts and pages in your backend have a new Search Exclude option at the bottom of the right-hand side menu. By checking the Exclude from Search Results box, you can exclude specific posts and pages from search results.
If you want to access the full list of pages and posts you’ve excluded from search results using the plugin, you can find your way to Settings > Search Exclude. You’ll find the list there, and you’ll also be able to remove posts and pages from the list by unchecking the appropriate box and clicking on the Save Changes button.
As plugins go, this one is simple and lightweight, but also pretty short on features. There are a couple more ways you can exclude search results that the plugin doesn’t cover. And you know what that means — it’s custom code snippets time!
To exclude from search results posts and pages made by specific authors, you’ll have to add some custom code to your website. The two methods we’d recommend for this are creating a child theme and adding the code to its functions.php file or using the Code Snippets plugin without the child theme.
Either way, you’ll need to find the author’s ID first. To do that, you can navigate to Users > All Users, and hover over the author you want to exclude with your mouse. In the bottom right-hand corner, you’ll notice a link appear — that link holds the author ID number.
With that number in mind, you can go ahead and add the following code using your preferred method:
function my_search_filter( $query ) { if ( $query->is_search && !is_admin() ) $query->set( 'author','-5' ); return $query; } add_filter( 'pre_get_posts', 'my_search_filter' );
Instead of the number 5, you should insert the ID number of the author you want to be excluded from the results. If you want to exclude multiple authors, list all their ID numbers separated by commas. It’s as simple as that.
When excluding tags and categories, your first step should be the same — finding out the ID number of the category or tag that you want to exclude. Head over to Posts > Categories for categories, or Posts > Tags for tags, and do the same thing you did to find out the author’s ID — hover over the category or tag you want to exclude. You’ll find the ID number in the link that appears in the bottom right-hand corner.
If it’s a category you want to exclude, you can use the following code:
function my_search_filter( $query ) { if ( $query->is_search && !is_admin() ) $query->set( 'cat','-21' ); return $query; } add_filter( 'pre_get_posts', 'my_search_filter' );
Of course, you should replace the number 21 with the correct ID for your category. Just like with the authors, you can exclude multiple categories by listing their ID numbers separated by commas.
For tags, the code looks almost exactly the same:
function my_search_filter( $query ) { if ( $query->is_search && !is_admin() ) $query->set( 'tag','-24' ); return $query; } add_filter( 'pre_get_posts', 'my_search_filter' );
Again, you should ensure to include the proper ID number for your tag and use the correct method for excluding more tags.
You’ll have to use slightly different code if you want to add custom taxonomies to the excluded bunch:
function my_search_filter( $query ) { global $wp_the_query; if( $query === $wp_the_query && $query->is_search() ) { $tax_query = array( array( 'taxonomy' => 'mammals', 'field' => 'slug', 'terms' => 'sloths', 'operator' => 'NOT IN', ) ); $query->set( 'tax_query', $tax_query ); } } add_action( 'pre_get_posts', 'my_search_filter' );
Instead of “mammals,” you’ll have to provide the custom taxonomy you’ve set. The “sloths” part stands in for the specific terms you want to exclude from the search results.
Let’s Wrap It Up
Being able to exclude specific results from a WordPress search can come in handy. The best-case scenario is that it saves you from having to install a search plugin. Usually, however, it will give you some control over who can find what on your website, and it’s up to you to make the most of it. Just remember that, in this case, third-party plugins will only get you halfway to your goal. You’ll have to traverse the rest on your own, one line of code at a time.