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.
[sourcecode]function vibeExcludePages($query) {
if ($query->is_search) {
$query->set(‘post_type’, ‘post’);
}
return $query;
}
add_filter(‘pre_get_posts’,'vibeExcludePages’);[/sourcecode]
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!
Popularity: 52% [?]
Related posts:





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!
Can you please post the plugin you are referring to? I need to do the same thing and exclude certain pages. Thanks!
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.
this will make search in posts from the control panel is not possible until you remove the code from the page.
Thanks for sharing! This is perfect for what I needed.
Was just looking for how to do this and stumbled upon this! thanks for the help!
Fantastic! Thank you *so* much for posting this!
Working with me, thank you so much been looking for this.
Oh, man thanx. This was the only code that actually worked. Thanx a lot
Hi,
I added the code to functions.php and reloaded the page. I got HTTP 500 Internal Server Error. I backed out the changes and I still get HTTP 500 Internal Server Error!
Log file says:
[Mon Mar 07 18:39:03 2011] [error] [client 81.138.23.186] PHP Fatal error: Cannot redeclare require_wp_db() (previously declared in /home/sites/internetsalesmarketing.co.uk/public_html/wp-includes/load.php:323) in /home/sites/internetsalesmarketing.co.uk/public_html/wp-includes/functions.php on line 3090
Can anyone help?
Thanks for such a great article! I hate using plugins for simple stuff like merely excluding pages from search.
It works, but it also makes the search in your Media Library not work anymore.
Here´s one way how to remove pages or categories ..
http://tocs-i.com/blog/exclude-pages-or-include-specific-categories-in-wordpress-search/