If you’re like me, your blog is probably focused around specific topics and you want people to find them. However, sometimes you write an article or post that isn’t directly related to what your blog normally caters to. You just “put it out there”, but it doesn’t form a part of your blog theme. I’ve written in the past on how to exclude certain categories from showing up on your home page, but how do you stop them from appearing in your blog search results?

Keep in mind that we don’t want them to stop appearing inĀ search engine results! We just want to exclude these individual posts and pages from showing up when people use the search function on your blog. Maybe your WordPress blog is focused around a specific product and when people search for something, you don’t want your unrelated post to clutter up their shopping experience and distract them from what they want.

In this article, I’m going to show you how to stop certain pages and posts from being part of your blog search results with a few short lines of code.

Example of an Unrelated Post Showing up in Search

As an example, I’ve created a test blog post with unrelated stuff as you can see in this screenshot here:

Once it’s published, it’s indexed by WordPress and when you do a search containing some words in the post, it shows up as expected like this:

So here, we searched for “fans” and we got a result that explicitly has nothing to do with fans! How do we correct this? Well, there are different ways of going about it. We can either:

  • Exclude posts and pages on an “ad hoc” basis by maintaining a list of pages we want to exclude
  • Create a new category for posts that we want to push out of the search results

Let’s look at the first example. To create a list of posts or pages you want to exclude from search, we first need to get their IDs. We can do that by opening up the list of posts and hovering over the “Edit” button and viewing the link in the status bar below. We then search for the post number as shown here:

In this case, the post number is “4711”. Open up your functions.php file or the place where you keep your custom PHP code and paste the following:

function exclude_pages_from_search($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $query->set('post__not_in', array(4711));
    }
  }
}

add_action('pre_get_posts','exclude_pages_from_search');

Here, we hook into the “pre_get_posts” action and use the following line to exclude the page by id:

 $query->set('post__not_in', array(4711));

You can add more pages to this list by adding their IDs separated with a comma. With this code in place, the page in question no longer appears in the search results:

Creating a Separate Category for Posts to be Excluded

Using the above approach, you need to modify the code each time you want to prevent a page from appearing in search. If this is a “one off” thing, then it’s fine. But if you do it somewhat regularly, you might want a long term solution. The idea is to create a new category altogether and assign excluded posts to that category at the time of creation. Then we get the category ID by going to Posts->Categories and hovering over the “Edit” link like this:

Here, we see the category called “NR” has a tag number of 31. To exclude this category, simply change the query->set line to:

$query->set( 'cat', '-31' );

Note how we place a minus (-) in front of the number to indicate that we want to exclude it. Without the minus, WordPress will think we want to show resultsĀ only from category 31!

Now it’s much easier to exclude posts on the fly without making changes to your code. Simply place a checkmark next to the category and you’re done!

These two solutions will allow you to write unrelated content on your blog without worrying about whether or not they will show up in the search results.

tracking pixel

Is your website slow?

Enter its URL below to find out now:

About the Author

Bhagwad Park

Leave a Reply

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