How to Exclude Pages from WordPress Search Results

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 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 file of your WordPress theme (sans the opening PHP tag):

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!

Enjoy? See Inside my WordPress Toolbox

Over 20,000 people have purchased my WordPress products. Get absolutely FREE access to regular updates and my toolbox - a collection of WordPress-related products and resources that every WordPress professional should own.

  • 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 :)

    • Thomas Griffin

      Awesome Samuel!

  • 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.

    • 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 I am unable to exclude Mediafiles from search results.


    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

  • Pingback: How to Include Custom Post Types in WordPress Search Results()

  • Thanks

    Confirmed that this works on current WP version… Just implemented. Thanks!