When you are searching WordPress, by default, the WordPress search displays all published posts and pages within the site that are relevant to your search. However, when users are using the WordPress search function, more often than not, they are looking for a particular post and not a page on the site.
In this tutorial, I will demonstrate two straightforward ways to exclude posts or pages from WordPress search results. We will use the plugin technique, and we will also go over how to easily add some code into your functions.php file that will allow you to exclude WordPress posts or pages automatically when someone is performing a search on your website.
Why Exclude Posts or Pages From Search?
There are actually a number of reasons why you may want to exclude posts or pages from a WordPress website search.
Here are a couple of the main reasons:
- You are publishing a private post or private page content for specific users, and you don’t want everyone to be able to find this content on your website.
- The site includes paid content that shouldn’t be accessible to users who don’t have permission to view it.
- You are trying to avoid irrelevant pages from showing up in search results on your site. A good example of this is the Homepage or Author Page showing up in results. Pages like those are usually not going to be relevant to a visitor’s search.
Basically, when you exclude posts and pages in WordPress search, you are allowing people to find relevant content faster and easier. It is not difficult to accomplish these changes.
Let’s take a look at how easily we can exclude posts or pages from a WordPress website search.
Exclude Posts or Pages from WordPress Search Using a Plugin
In order to exclude posts or pages form a WordPress website search using the plugin method, we are going to use the Search Exclude plugin.
Search Exclude is a lightweight, simple-to-use plugin. It allows you to quickly and easily exclude any posts or pages from a blog search in WordPress.
The plugin has no settings to configure.
Essentially, it adds a new feature to the editing screen for posts and pages. You simply install and activate the plugin, and you can exclude pages by checking the checkbox that now appears within posts and pages.
Search Exclude also provides you with a “settings” page area that will list all the posts and pages you have excluded from the search. This way, you can easily find what you are looking for without going through all your posts or pages one-by-one.
Note: This plugin does support quick and bulk edit. It will also not affect your SEO in any way. Your sitemap will still include the pages or posts that you exclude from search.
Install and Activate Search Exclude
In order to use the Search Exclude plugin, it needs to be installed and activated. You can do this from the plugins page of your WordPress admin dashboard. Simply search for the plugin.
Once you have located the plugin, click the “Install Now” button, then click the “Activate” button.
Once the plugin has been installed and activated, open any post or page in the editor. You will see that a new checkbox appears on the right side of the editor titled “Search Exclude.”
To exclude a post or page from a WordPress search results page, simply check the relevant box and then publish or update the content. Now the post or page will be excluded from the search results on your WordPress site.
To see a list of all the posts and pages you have excluded over time, click on Settings > Search Exclude.
Now you see a list of all the posts and pages you have excluded from your WordPress website search. This is an easy way to track and see everything you have done.
For example, maybe you see a sharp drop in traffic of a post that you wanted to remain within the site search. You can look in this list to ensure it wasn’t accidentally added.
Adding Code to Functions.php File to Exclude Posts or Pages
You can also remove posts or pages from your blog search in WordPress by adding some code to the functions.php file.
Note: If you edit your theme’s functions.php file, you run the risk that your changes will be overwritten when the theme updates. For that reason, it’s always best to use and edit a child theme whenever you plan to make changes to theme files.
To get to your website’s functions.php, file click on Appearance > Editor.
This will take you to the WordPress website files where you can edit your code. Click on “Theme Functions (functions.php),” and you will be ready to place the correct code to exclude WordPress pages.
Place the following code into your theme’s functions.php file:
//Alter the WordPress search to return ONLY posts, no pages if (!is_admin()) { function search_filter_posts($query) { if ($query->is_search) { $query->set('post_type', 'post'); } return $query; } add_filter('pre_get_posts','search_filter_posts'); }
What Does The Code Do?
Simply put, the code above checks to make sure that the WordPress search is not originating from any of the WordPress admin pages. Once it verifies that it isn’t, then the code forces searches for posts only by setting the post_type parameter and will exclude WordPress pages.
If you would like to do the opposite, force the WordPress search results page to show only pages, set the post_type parameter to “page.”
//Alter the WordPress search to return ONLY pages, no posts if (!is_admin()) { function search_filter_pages($query) { if ($query->is_search) { $query->set('post_type', 'page'); } return $query; } add_filter('pre_get_posts','search_filter_pages'); }
The Effects of Removing Something From Search Results
Private content or paid content isn’t generally part of your site statistics or analytics, so removing them from search won’t have a negative impact. On the other hand, removing a page or post from your onsite search results is likely to cause a drop in viewership.
That’s to be expected, but as I mentioned earlier, your sitemap should still contain the pages you exclude from search. So if your SEO game is strong, the effect of removing a page (or all pages) from search shouldn’t be overly negative.
Just remember that you are reducing the visibility of anything you exclude from search. So it’s a good idea to revisit your statistics or analytics a few weeks or months after making any exclusions.
Final Thoughts
Regardless of your reasons for wanting to remove something from search, the flexibility of WordPress makes it easy to do. Plugins are there for ease and speed, and template or file modifications for those who prefer to dig into the works.
That’s why we covered both methods in this tutorial. One method is not necessarily better than the other, it’s just a matter of your preference and experience level.
In addition to what we’ve talked about here, there is also a great option to tell your WordPress site to use more search engines when performing a search on the site. I hope this tutorial gave you a little insight into how easy it is to exclude posts or pages from WordPress website search.
Have you used the above plugin technique or code before? What is your preferred method of improving search functionality on your WordPress website?
great …. i exclude pages from search result….