I’m documenting a fix to a problem I’ve fixed on the SideGains blog today. It relates to the native WordPress search function that pulls both pages and posts into the results page… I want to exclude pages from WordPress search results to show only posts.
Why do I want to do this?
Since my pages are accessible from my navigation menu, I feel it’s unhelpful to have them returned in a search. I only want search results to retrieve the blog posts most relevant to my what my visitors are looking for.
For me, having pages in search results just muddies the waters.
I also don’t want to add a plugin to do this for me. Read the following posts to understand why:
So in this post, I’m going to explain how to exclude pages from WordPress search results. This ensures the search function only returns relevant blog posts as results.
This tutorial requires you to add several lines to your function.php file. This is an important file and so you need to be careful whenever you edit it. If you break the file it can bring down your WordPress blog.
Be sure to use a WordPress child theme and make copy of your functions.php file before you change it. If something goes wrong, you’ll be able to restore the working copy to fix it.
Editing Your functions.php File to to Exclude Pages from WordPress Search Results
The following code should work for any WordPress version and theme. I use the Genesis Framework for the SideGains blog, though I have tested this on other themes.
- The first step is to locate your function.php file and make a backup copy.
- Go to the end of the functions.php file and add the following code:
/** Exclude Pages from WordPress Search Results Without a Plugin **/
if (!is_admin()) {
function wpb_search_filter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','wpb_search_filter');
}
3. Save the file.
That’s it!
Now try running a search on your WordPress blog and check to see that no pages display in the results.
An Explanation of How This Function Excludes Pages from WordPress Searches
The first “if statement” checks whether the search has been made in the admin panel, and if it has, it ignores the function. This is important because we don’t want to exclude pages from searches we make within your WordPress admin panel… we only want the function to affect searches made by your visitors.
Assuming someone used the search tool on your blog (and not within the admin panel), the function forces the WordPress search function to return only posts in the results. This is achieved by requesting the ‘post’ within the ‘post_type’ parameter.
As an aside, if you only wanted your WordPress search function to return pages only in the search results, you just need to set the ‘post_type’ parameter to ‘page’ instead of ‘post’.
Summary
There are lots of reasons why you’d want to include or exclude pages from WordPress search results. For me it was a question of only returning my blog posts that match a visitor search. In my view, it’s posts rather than pages that my visitors will want to see.
Of course, you might have a different reason or need, and as a consequence you may want to exclude posts from searches and only return pages.
I hope this short tutorial helps you exclude pages from your WordPress search results. Of course if you feel uncomfortable editing your functions.php file you could always search WordPress for a plugin to do the job for you.
That’s all for now.
Paul

Has this helped you to exclude pages from your WordPress searches? Perhaps you have another way to achieve this? Please feel free to ask a question or drop me a comment below.
Leave a Reply