Exclude Pages From WordPress Search Results

magnify

Image by Szorstki

As WordPress is being used more and more as a content management system, the role of Pages become clearer. Most of the time, I’d use Pages for opt-ins like this one and this. Pages are also good for newsletter subscription thank yous, or special landing/offer pages that you don’t link to from the main areas of your site and only release for specified groups of people.

Removing these Pages from your site is a small issue – you can either edit your theme or use a plugin to remove the links but try searching your site using WordPress’ built in search. You’ll find it pulls up matching Pages that you don’t want people to see. Not great.

While there are plugins like Search Unleashed which can help you do that, they often do a bit too much than you need it to. That’s true for me. I don’t need or want extra features, just to remove Pages from the search results. Here’s the method I am using. You enter this code in your theme’s functions.php file – or create the file if the theme doesn’t come with it. If you change themes, you will only have to copy it over to the new theme’s functions.php file.

function mySearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','mySearchFilter');

Do You Want A Hands-Free Business?

Then get this guide to help you systemize your business so you'll have more time working on your business.

!
!

Hey! I want to make sure you know what you're getting here. In addition to the guide, you will also receive our memo that includes special offers, announcements and of course actionable information.

Terms and Conditions checkbox is required.
Something went wrong. Please check your entries and try again.
Facebook Comments

4 Comments

  1. WebpageLottery on February 25, 2010 at 2:06 pm

    Finally, I found something works.
    Thank you very much.



  2. Lynette Chandler on February 25, 2010 at 2:34 pm

    You're welcome



  3. WebpageLottery on February 25, 2010 at 6:06 pm

    Finally, I found something works.
    Thank you very much.



  4. Lynette Chandler on February 25, 2010 at 6:34 pm

    You're welcome