Excluding Pages from wordpress search results

By deafult WordPress search feature displays pages as well as posts in the search results. It may be desirable that you want to display only posts for the search results in your blog. Here how you can do this.

Create the function below and add it to your theme functions.php file.


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

add_filter('pre_get_posts','SearchResultsFilter');

Leave a Comment

Back to top