Exclude Pages From WordPress Search Results



WordPress Search feature displays published posts and pages in search results, by default. But when users search in a blog for something, it is most likely a post rather than a page. So in this tutorial, I will show you how to exclude pages from WordPress search results and make your search more relevant.

Open your theme’s functions.php file and paste following code:

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

add_filter('pre_get_posts','mySearchFilter');

This code searches for posts only through setting the post_type. You can also make it do the reverse by setting the post_type to pages, so it will return only pages in the search results.