• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Thomas Griffin

Be a better leader

  • About
  • Newsletter
  • Articles
  • Business
  • Favorites
  • Contact

How to Exclude Pages from WordPress Search Results

Thomas Griffin Avatar
written byThomas Griffin
last modifiedJanuary 29, 2023

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. For most websites, this is not necessary or desired. In order to change this behavior, 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:

How to Exclude Pages from WordPress Search Results
/**
 * This function modifies the main WordPress query to remove 
 * pages from search results.
 *
 * @param object $query The main WordPress query.
 */
function tg_exclude_pages_from_search_results( $query ) {
    if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
        $query->set( 'post_type', array( 'post' ) );
    }    
}
add_action( 'pre_get_posts', 'tg_exclude_pages_from_search_results' );

This function does exactly what we need it to do. By modifying the main WordPress query via 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, not our pages.

Exclusive WordPress Offer

Want a WordPress website that’s secure AND fast? My friends at WP Engine are offering 3 months free on all annual plans. Click here to claim your special WP Engine offer!

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 get more great WordPress content! Also, check out the follow up post on how to include custom post types in WordPress search results!

FacebookTweetLinkedIn
Thomas Griffin Avatar
Thomas Griffin

Hello! I'm Thomas, and I've been in WordPress for 10+ years. I've contributed to core and created the TGM Plugin Activation class. My WordPress plugins are active on over 19,000,000 websites and include popular ones like OptinMonster, WPForms and MonsterInsights.

View my WordPress toolbox•More about me →

Reader Interactions

Comments

  1. Clarus Dignus

    March 24, 2022

    Works great. Thanks for this function and for presenting it concisely.

    Reply
    • Thomas Griffin

      April 14, 2022

      You’re quite welcome! Now those pages will stay away from your WordPress search results!

      Reply
  2. FD

    March 25, 2022

    Thanks Thomas. Very useful. Saved my day.

    Reply
    • Thomas Griffin

      April 14, 2022

      You are quite welcome! Glad you found it helpful to exclude pages from your search results. 🙂

      Reply
  3. BT

    May 7, 2022

    It works. Thank you. Thank you. Thank you. 😊

    Reply
  4. Sayan Dutta

    August 15, 2022

    Works great. Thanks

    Reply
  5. chad

    November 3, 2022

    How do you make wordpress show both pages and posts in the search?

    Reply
    • Thomas Griffin

      November 4, 2022

      Hey Chad – this is how WordPress search works by default. If you want to keep pages in search along with other custom post types, you can just include 'page' as part of the array when you customize the post types.

      Reply
  6. Alexandra

    November 30, 2022

    It excludes posts as well.

    Reply
    • Thomas Griffin

      November 30, 2022

      Make sure you’ve copied it as stated and that no other plugins are interfering with the ability to exclude pages from the search results. It should work as anticipated!

      Reply
  7. Alice William

    December 16, 2022

    Any idea how to exclude one particular page from search?

    Reply
    • Thomas Griffin

      December 19, 2022

      Just underneath the other query modification, you would add something like this:

      $query->set( 'post__not_in', array( 10 ) );

      Change out 10 for the ID of the page you want to exclude from your search results.

      Reply

Leave a Reply to Alice William Cancel reply

Your email address will not be published. Required fields are marked *

previous article: Welcome to Leadership
next article: How to Include Custom Post Types in WordPress Search Results
Breakthrough Leadership: 5 Surprisingly Simple Qualities of Today's Exceptional Leader

Breakthrough Leadership

Great leaders share common traits that separate them from the pack. Download your copy of my signature ebook, Breakthrough Leadership: 5 Surprisingly Simple Qualities of Today's Exceptional Leader, absolutely FREE when you enter your name and email address below.

The way to get started is quit talking and begin doing.

Walt Disney

Footer

ThomasGriffin.com Logo

Copyright © 2010-2023 Griffin Media LLC.
All rights reserved.

Connect

Sites I Like
OptinMonsterTrustPulseWPFormsMonsterInsightsWPBeginnerEclectic TigerSyed BalkhiAwesome MotiveT214

Misc
WordPressPrivacyDisclosures Sitemap

The Better Leader Newsletter

The Better Leader Newsletter

Join 6,889+ leaders taking the right steps to grow in their leadership. No spam - just real, thought-provoking stories and advice to make you a better leader. Grow in your leadership by subscribing today!