How to remove pages from search results in WordPress

Removing pages from the search results can be a plus, specially when your visitors are looking for the rich content and not a page.

Open wp-content/themes/your-theme/functions.php, add the following:

// Removes pages from search results
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}

add_filter('pre_get_posts','SearchFilter');

Simply remove the above code if you wish to allow pages to be shown again in search results.

Date: Jun 18, 2012

Filed Under: Web Development

About the Author: Computer Repair, Freelancer, Blogger, Reviewer and all around nice guy!

  1. emre says:

    thx, it worked :)

Leave a Reply




Google PageRank