Soliloquy - The Best Responsive WordPress Slider Plugin

How to Exclude Pages from WordPress Search Results

Share This Post

Today I want to share with you a quick and easy tutorial on how to exclude pages from WordPress search results.

By default, WordPress will include your site’s pages in its search results, which can sometimes be annoying and quite frankly irrelevant to your user’s needs. In order to combat this, we can add simple function into our functions.php (or custom_functions.php for Thesis users) file in order to filter out those pages in our search results.

The Function to Exclude Pages from WordPress Search Results

Simply copy and paste this function into your functions.php or custom_functions.php file:

This function does exactly what we need it to do. By placing a filter on the pre_get_posts hook, we can determine what we want to show in our search results. In this case, all we want to show is content from our blog posts, so we simply define that variable (and only that variable) for the query.

And there you have it! Simply save your file and test out a search query. You have excluded pages from your WordPress search results!

Hope you enjoyed the post, and don’t forget to subscribe to these tips and tutorials in the section below! Also, check out the follow up post on how to include custom post types in WordPress search results!

Have you enjoyed reading How to Exclude Pages from WordPress Search Results? Sign up below to get regular content updates and also get my "7 Tips to a Professional WordPress Setup" absolutely free!

About Thomas Griffin

I live and breathe WordPress. I create products around WordPress (Soliloquy and OptinMonster), contribute to WordPress core and do lots of fun development around WordPress in general. You can find me on Twitter, Facebook and Google+.

  • Samuel

    I removed my search box because I didn’t my pages to appear, now that I know how to exclude pages, I can put back my search box, all thanks to you :)

    • http://thomasgriffinmedia.com Thomas Griffin

      Awesome Samuel!

  • http://www.moreclientsplease.com Matthew

    This is EXACTLY what I’ve been looking for. Thanks for taking the time to do this.

    I was worried about protecting downloadable content. Although this isn’t going to deter the determined, it will do for me!

    Thanks again.

    • http://thomasgriffinmedia.com Thomas Griffin

      That’s wonderful Matthew! I’m glad it has helped you achieve what you were looking for!

  • Paul

    Yes, it excludes pages from search but it also excludes everything else (post types). Post should be named “How to include posts ONLY in search and exclude everything else”.

  • AB

    Thanks! Great tip!
    But… How could I exclude exact post or page from search results?

  • rameez

    Hi, i have this blog http://www.cloudreviews.com/blog. I am unable to exclude Mediafiles from search results.

    i.e http://www.cloudreviews.com/blog/?s=photo

    Result shows me posts as well as Mediafiles (images) whose title matches with my seach query.
    i am using pageline theme and its structure is quite complicated.
    looking for you help. thanks