• 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 modifiedJune 11, 2021
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. 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:

/**
 * 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

Hey friend! 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

Leave a Reply Cancel reply

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

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.

Clear over clever.

Thomas Griffin

Footer

ThomasGriffin.com Logo

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

Connect

Sites I Like
OptinMonsterTrustPulseWPFormsMonsterInsightsWPBeginnerEclectic TigerSyed BalkhiAwesome Motive

Misc
Don't Click HereWordPressPrivacyDisclosures Sitemap

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.