• 
  • 
  • 
  • 
  • 
  • 

WPSpeak.com

  • Home
  • Tutorials
  • Blog
  • Categories
    • BuddyPress
    • Design
    • Giveaways
    • News
    • Plugins
    • Reviews
    • Roundups
    • Snippets
    • WordPress Multisite
  • Themes
/ Snippets / How To Exclude Pages From Search Result

How To Exclude Pages From Search Result

UPDATED: Aug 11th, 2014

On of the important features needed in every WordPress site is the search function. This allows users to search your website with any keyword. However, when someone search your site, WordPress does not exclude pages from the search result. Here’s an example.

In this example, I searched for “contact form”, so the search result page will list any post and page that contains the word “contact”. As you can see, it will also show my Contact page as one of the results, with no excerpt.

Here’s a little snippet to exclude pages from search result:

<?php
/** Exclude Page in Search */
function exclude_search_pages($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','exclude_search_pages');
view raw functions.php hosted with ❤ by GitHub

Please note that this only exclude the pages when someone search on your site. It does block any Google bots from crawling any page.

Related

Snippets 2 Comments

Comments

  1. Vivek R says

    June 3, 2024 at 12:58 am

    I’m wondering how it excludes the page from Search Engine crawlers.

    Reply
    • Editor says

      June 3, 2024 at 11:35 am

      If you use any seo plugin like Yoast and AISEO, I’m prettty sure in the seo metabox for every page, there’s an option to set the page to NOINDEX.

      Reply

Leave a Reply Cancel reply

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

Get Latest Updates

Subscribe to get latest updates on WordPress Tutorials, News, Reviews, Code Snippets and everything in between. No spam. Promise.

Teslathemes-300-250

Categories

  • BuddyPress
  • Design
  • eCommerce
  • Giveaways
  • News
  • Plugins
  • Reviews
  • Roundups
  • Snippets
  • Tutorials
  • WordPress Multisite

About Us

WPSpeak is a site dedicated for WordPress news, tutorials, code snippet, themes and plugins. Founded in January 2013, it's aimed to provide high quality WordPress resources for all WordPress users. All rights reserved.

Contact Us

  • Email
  • Twitter
  • Facebook
  • Google+

Recent Themes

  • Concise Theme
  • Minimal Theme
  • Muse Theme
  • Personify Theme

© 2013–2015 · Powered by WordPress and Genesis Framework