How to Exclude Pages from Search Results

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!

Get involved, Vibe with us.

26 Vibes for this post.

  1. AlexSchleber says:

    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.

  2. Digidoll says:

    Thank you soooo much for this! Looked around everywhere & this was the only thing that worked ;)

  3. Robert says:

    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?

  4. Shaun says:

    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.

  5. arveerella says:

    wow… something that finally works! ♥ thanks… =)

  6. Dinesh says:

    Thanks for this, its really important to remove pages from search results cause you don’t want contact, advertise to display among your posts.

  7. r4 kaart says:

    I have tried it but i receive an error: Fatal error: Call to undefined function add_filter()

    any idea?

  8. Duncan Cross says:

    Thanks so much – this is awesome. I have tried other filters with no luck; yours is the first thing that works for me.

  9. Matt Pealing says:

    Works like a charm!

    Thanks a lot ;)

  10. KPOP7.com says:

    Thanks alot, finally something that works.

  11. moe says:

    this will make search in posts from the control panel is not possible until you remove the code from the page.

  12. Joseph says:

    Thanks for sharing! This is perfect for what I needed.

  13. Andrew Reff says:

    Was just looking for how to do this and stumbled upon this! thanks for the help!

  14. Amy R says:

    Fantastic! Thank you *so* much for posting this!

  15. Hoda says:

    Working with me, thank you so much been looking for this.

  16. Andrei says:

    Oh, man thanx. This was the only code that actually worked. Thanx a lot

  17. Neil says:

    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?

  18. Web design says:

    Thanks for such a great article! I hate using plugins for simple stuff like merely excluding pages from search.

  19. Andrew says:

    It works, but it also makes the search in your Media Library not work anymore.

Trackbacks

  1. 11 Awesome Wordpress Development Blogs
  2. More and more Mortals uploaded | DUNCAN CROSS