How to Exclude Posts, Pages, Categories or Authors From WordPress Search

In this tutorial, I will show you how to use a plugin to exclude posts, pages, categories or authors from your WordPress search results.

When you search in WordPress, the results include all of the relevant posts and pages. However, you may have some situations where excluding a post or page, or even an author or category from search results is more ideal.

This article will show you how to do that easily with a WordPress plugin.

If you need to take customization even further, I will also point you to an article that shows you how to edit WordPress files to set search exclusions based on other criteria.

Exclude Posts and Pages From WordPress Search Results

The first step is to install the Search Exclude plugin.

Log in to your WordPress admin panel. In the left side of the WordPress admin panel, mouseover the “Plugins” link and click the “Add New” link.

how to exclude posts or pages from WordPress search step 1

Search for “Search Exclude,” click the “Install Now” button

how to exclude posts or pages from WordPress search step 2

Click the “Activate” button.

how to exclude posts or pages from WordPress search step 3

To Exclude a Post From WordPress Search Results

Go to your list of posts and Click the “Edit” link for the post you would like to exclude.

how to exclude posts or pages from WordPress search step 4

Scroll down to the bottom right of the post. Check the box for “Exclude from Search Results.”

how to exclude posts or pages from WordPress search step 5

Scroll up and click the “Update” button.

how to exclude posts or pages from WordPress search step 6

The post will now be excluded from the WordPress search results.

To Exclude a Page From WordPress Search Results

Excluding a page is done the same way you exclude a post. Go to your list of pages and click the “Edit” link for the post you would like to exclude.

Scroll down to the bottom right of the post. Check the box for “Exclude from Search Results.”

Click the “Update” button.

how to exclude posts or pages from WordPress search step 7

The page will now be excluded from the WordPress search results.

View or Edit the Pages and Posts Excluded From WordPress Search Results

Mouseover the “Settings” link and click the “Search Exclude” link.

how to exclude posts or pages from WordPress search step 8

The list will include posts and pages excluded from WordPress search results.

To stop excluding a post or page, uncheck the box for the post or page and click the “Save Changes” button.

how to exclude posts or pages from WordPress search step 9

That’s a pretty easy method to exclude posts and pages from WordPress search results. What if you want more advanced exclusions? For example, what if you need to exclude a specific category or author from search results? That’s a bit more complicated, but it’s possible with a few lines of code.

To Exclude a Category or Author From WordPress Search Results

There are a couple of different ways to exclude categories or authors from your search results. First, you can edit functions.php, one of the files in your WordPress theme. The other method is to create your own plugin to do the job. Creating a plugin sounds like a difficult task, but it’s pretty easy. Check out our tutorial, How to Create a Custom Site-Specific WordPress Plugin.

The benefit of making your own plugin is the code remains separate from the theme you’re using. So if you change themes, or update an existing theme, your code doesn’t get lost in the process. Of course, you can also protect your changes by creating a child theme, and making your functions.php changes in the child theme.

Whichever way you choose, you always want to avoid editing any theme files directly. If you do that, your changes will be overwritten when the theme is updated.

Excluding Specific Categories From WordPress Search Results

Before we get to the code, let’s look at how to identify a specific category.

Log in to your WordPress admin panel. Mouseover “Posts” and click the “Categories” link.

how to exclude posts or pages from WordPress search step 10

Mouseover the category you want to identify. The category ID is in the URL (in this example, the ID is 5).

how to exclude posts or pages from WordPress search step 11

Now you’re going to add the following code to functions.php. You can do this by using FTP programs like FileZilla by downloading, editing and uploading functions.php from your theme directory.

If you don’t want to use FTP, you can use the WordPress admin panel. Mouseover “Appearance” and click the “Theme Editor” link.

Whichever method you use, be sure to edit the functions.php file in your child theme, not in the main theme directory.

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]function wpb_search_filter( $query ) {
if ( $query->is_search && !is_admin() )
$query->set( ‘cat’,’-5‘ );
return $query;
}
add_filter( ‘pre_get_posts’, ‘wpb_search_filter’ );[/ht_message]

In the code above you will replace “-5″ with the ID for your category. You can exclude multiple categories from the search results by separating the IDs with commas:

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]function wpb_search_filter( $query ) {
if ( $query->is_search && !is_admin() )
$query->set( ‘cat’,’-5, -7, -10‘ );
return $query;
}
add_filter( ‘pre_get_posts’, ‘wpb_search_filter’ );[/ht_message]

Excluding Specific Authors From WordPress Search Results

Excluding authors is done in the same way we excluded categories. First, identify the author ID.

Mouseover “Users” and click the “All Users” link.

how to exclude posts or pages from WordPress search step 12

Mouseover the author you want to identify. The author ID is in the URL (in this example, the ID is 8).

how to exclude posts or pages from WordPress search step 13

Then add the following code to functions.php:

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]function wpb_search_filter( $query ) {
if ( $query->is_search && !is_admin() )
$query->set( ‘author’,’-8‘ );
return $query;
}
add_filter( ‘pre_get_posts’, ‘wpb_search_filter’ );
[/ht_message]

In the code above you will replace “-8″ with the ID for the specific author you wish to exclude. You can exclude multiple authors from the search results by separating the IDs with commas:

[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]function wpb_search_filter( $query ) {
if ( $query->is_search && !is_admin() )
$query->set( ‘author’,’-8, -6, -4‘ );
return $query;
}
add_filter( ‘pre_get_posts’, ‘wpb_search_filter’ );
[/ht_message]

That’s All There Is to It

Now you know how to use a plugin to exclude posts or pages from your WordPress search results, as well as the code necessary to exclude categories or authors.

I hope you find these new tools helpful in customizing and fine-tuning your WordPress installation.

How many customizations do you use with WordPress? Do you prefer using plugins or adding code to theme files?

2 thoughts on “How to Exclude Posts, Pages, Categories or Authors From WordPress Search”

  1. 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.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.