How to Exclude Specific Results from WordPress Search 1

How to Exclude Specific Results from WordPress Search

When trying to provide your website visitors with a way to navigate your website, the two elements that grab your attention the most are the navigation menu and search functionality. Adding the navigation menu can be very easy. The search bar is another widget that you can easily add. However, keep in mind that both require fine tuning to shine fully.

As for the search bar, your first instinct might be to reach for a WordPress search plugin and use it to improve native search functionality. But before you do that, make sure you have exhausted your other options in WordPress. Excluding search results is one of them.

In this article, we will show you:

Why Do You Want to Exclude Certain Results From Search?

The primary role of the search function is to generate results based on a user’s query. If you want the feature to be useful and add to the overall user experience, you want the results to be as relevant and zeroed as possible.

One of the ways to increase relevance is to remove often irrelevant results, such as the home page or about us page. You can also use search results exclusion to reduce clutter.. You can allow your visitors to search only a few posts and pages instead of the entire website.

Finally, excluding certain results can help you manage access to content on your website. If there are posts or pages you don’t want to be found easily, excluding them from navigation and search results would be an excellent way to hide them without actually hiding them.

How to Exclude Posts and Pages from Search Results

You can use a plugin to exclude certain posts and pages from your website’s search results. called Search ExcludeYou can easily find it by going to Plugins > Add New into the backend of your website and type its name in the search box. Once you find it, install it and activate it.

Search Exclude

Once enabled, you will notice that your existing posts and pages in your backend have a new Search Exclusion option at the bottom of the right-hand side menu. You can exclude certain posts and pages from search results by checking the Exclude from Search Results box.

Exclude Post

If you want to access the full list of pages and posts you have removed from search results using the plugin, you can find your way around. Settings > Search Exclude. You will find the list there and also Remove posts and pages from the list by unchecking the appropriate box and clicking the Save Changes button.

Search for Excluded Items

As plugins go, this one is simple and lightweight, but pretty short on features as well. There are several other ways to exclude search results that the plugin does not cover. And you know what that means – it’s time for custom code snippets!

How to Exclude Authors

To exclude posts and pages by specific authors from search results, you will need to add some custom code to your website. The two methods we recommend for this are to create a child theme and add the code to the theme. functions.php using file or code snippets plugin without child theme.

Either way, you’ll need to find the author’s ID first. To do this, you can go to: Users > All UsersClick and hover over the author you want to exclude with your mouse. You will notice a link appear in the lower right corner — this link holds author id number.

User ID

With this trick in mind, you can 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 must enter the ID number of the author you want to be removed from the results. If you want to exclude multiple authors, list all ID numbers separated by commas.. It’s that simple.

How to Exclude Tags, Categories, and Custom Taxonomy Terms

When excluding tags and categories, your first step should be the same – finding the ID number of the category or tag you want to exclude. get over your head Posts > Categories for categories or Posts > Tags do the same thing you did for the tags and to find the author’s ID — Hover over the category or tag you want to exclude. You will find the ID number in the link that appears in the lower right corner.

tag id

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 authors, you can exclude multiple categories by listing their ID numbers separated by commas.

The code for tags 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 make sure you include the appropriate ID number for your tag and use the correct method to exclude further tags.

If you want to add custom taxonomies to the excluded group you will need to use slightly different code:

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 will need to provide the specific classification you set. The “slackers” section refers to specific terms you want to exclude from search results.

let’s hug

Being able to exclude certain results from a WordPress search can be handy. The best case scenario is that it saves you from installing a search plugin. But it usually gives you some control over who can find what on your website and it’s up to you to get the most out of it. Just remember, in this case, third-party add-ons will only get you halfway to your destination. You’ll have to go through the rest on your own, one line of code at a time.

Support our work ❤️

If you enjoyed this article, consider leaving a tip to help us keep publishing great content.

Secure payment on PayPal
Moyens I/O Staff is a team of expert writers passionate about technology, innovation, and digital trends. With strong expertise in AI, mobile apps, gaming, and digital culture, we produce accurate, verified, and valuable content. Our mission: to provide reliable and clear information to help you navigate the ever-evolving digital world. Discover what our readers say on Trustpilot.