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.

Published by

Ryan Kessen

Computer Repair, Freelancer, Blogger, Reviewer and all around nice guy!

One thought on “How to remove pages from search results in WordPress”

Comments are closed.