WordPress by default will include pages in the search results. More often than not, you will want users to only be able to see posts in the search results, not pages.
To change this default behavior, it’s actually quite easy.
Include the following lines of code in your functions.php file.
function vibeExcludePages($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','vibeExcludePages');By adding this bit of code, it will basically tell your search results to not display anything but posts.
If you have any questions or comments, please leave them in the comments below.
Enjoy!











Thanks for this, exactly what I was looking for. The WP Query code is so darn convoluted…
This should really be an option in the core Settings, maybe under the Search Engine option or similar.
Cheers!
You do have to bear in mind that WordPress tries to keep the core as small as possible. With that in mind, the options it provides tries to be very slim, and then let’s a plugin handle the rest.
More often than not, a person won’t care if it’s in the search results or not, so it’s not a high priority to have options available for it.
It’s simple enough to use the code above and magically you now have a cleaner search function.
Thank you soooo much for this! Looked around everywhere & this was the only thing that worked ;)
Thanks for this, but I have a question? How do you get this to work with specific pages? Where/how can I exclude pages 9 and 14 for example?
Im looking for a solution to the same thing as Robert.
How do I specify specific pages or even catagories for that matter? ….More importantly pages though.
Thank you for this script though! Seems to work like a charm.
Shaun, I found this plugin that works pretty well for my purposes
Hopefully you have equal success!
wow… something that finally works! ♥ thanks… =)
Thanks for this, its really important to remove pages from search results cause you don’t want contact, advertise to display among your posts.
I have tried it but i receive an error: Fatal error: Call to undefined function add_filter()
any idea?
are you running the latest version of WordPress? Pretty much every version of WordPress has add_filter I believe.
Thanks so much – this is awesome. I have tried other filters with no luck; yours is the first thing that works for me.
Works like a charm!
Thanks a lot ;)
Thanks alot, finally something that works.