Search

How to Exclude a Page or Post from Search Results Page in WordPress

By default, the WordPress search result displays everything on your site including posts, custom post types, pages, categories and tags, titles, and content that are relevant to your search keyword. However, when users are using the WordPress search function, generally they are looking for a particular post and not a page on the site.

Your next question might be – “is our homepage and another irrelevant page to our visitor’s search will be displayed as well? “. Yes, they will be displayed as long as they have the relevant word on those pages. Well, it will be a bit turn-off for the visitor, right? That is one of the occasions when excluding pages or posts is needed.

Another example of the purpose of excluding search results is when you run a membership website, surely you wouldn’t want the subscriber-only content to show up in the search result of an anonymous user.

There are still many reasons you may want to exclude pages and posts, with that said, in this article, we will show you how to exclude posts and pages from WordPress search results.

How to Exclude a Page from Search Results

Let’s get into the more actionable steps of how to exclude search results in WordPress.

As is often the case with WordPress, there are two routes that you can follow to exclude search results:

  1. Using a plugin
  2. Using custom code

In almost all cases, using the plugin as a means to exclude search results is easier to do from the later method, so we’re going to start our tutorial there.

Excluding a Page from Search Results Using Plugin

First, you need to install the plugin. For this tutorial, we’ll use the Search Exclude plugin and you can get it from wordpress.org for free. There are some other plugins that can exclude search results as well, however, the Search Exclude plugin is lightweight, simple to use, and does perfectly for this job.

After installing the plugin, proceed by activating it and you can immediately exclude pages and posts with no setting required by checking the checkbox that now appears within the WordPress editor.

Note: Search Exclude plugin also supports quick and bulk edits.

Now if you want to know any pages or posts you’ve excluded over time from the search result, the Search Exclude plugin also provides you with a “settings” page that will list all of them.

Go to Settings in the WordPress dashboard then click on the Search Exclude menu. On the appearing page, you’ll find pages and posts you’ve excluded over time from the search result and you can make them available again for search just by ticking off the checkbox in the list and clicking on the Save Changes button.

Excluding a Page from Search Results Using Custom Code

To exclude pages or posts from search results with custom code, you will need to add the following snippet into your function.php file. We recommend using a child theme for this, to avoid the risk of our changes will be overwritten when updating the theme. And you could use the Code Snippets plugin to insert the code snippet seamlessly.

if (!is_admin()) {
  function search_filter_pages($query) {
    if ($query->is_search) {
  $query->set('post_type', 'post');
}
  return $query;
}
  add_filter('pre_get_posts','search_filter_pages');
}

The code snippet above makes the WordPress search only for posts after making sure it does not originate from any of the WordPress admin pages. For pages only search results, you can modify the 'post' parameter into 'page'.

 $query->set('post_type', 'post');

Now, go to Appearance on the WordPress dashboard then select the Theme File Editor menu, you will be taken to the Edit Themes page, then continue by clicking on the function.php in the list of Theme Files to open the function.php file.

Once you’re in the editor, you can paste the code snippet just under the PHP tag <?php. Here is what it looks like in our function.php file:

Continue by clicking the Update File button to complete the tutorial.

Now, when you try to search with the WordPress search function, the result will be only posts and no pages will be displayed.

This page may contain affiliate links, which help support our project. Read our affiliate disclosure.
Hendri Risman

Hendri Risman

Hendri is a WordPress expert and a writer staff at WPPagebuilders. He writes solutions on how to get things fixed in WordPress a lot. Mostly without involving a plugin.
Want to save yearly expense up to $219? why not?

2 thoughts on “How to Exclude a Page or Post from Search Results Page in WordPress”

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

    Reply
    • Hi Warszawa,

      In most cases, you don’t need to do so unless you change your theme. You can refer this post for a smarter way to add code snippet in WordPress

      Reply

Leave a Comment