How to Exclude Pages from Search Results in WordPress

If you have hidden pages on your blog, you may want to hide them from search results too. Here is a little trick to hide them all.

This trick uses pre_get_posts filter to remove pages from the query results. Here is the code:

// Remove pages from search results
function shailan_remove_pages($query){
	if ($query->is_search){
		$query->set('post_type','post');
	}
	return $query;
}
add_filter('pre_get_posts','shailan_remove_pages');

Open your functions.php file and put this snippet in. Save and upload.

After the edit is done, you can test it out by searching a page title.

This way you can make sure no pages are listed on your search results page.

Hope you like this trick. Enjoy!

Related Posts

NS Weight Release

WP-Backgrounds Lite by InoPlugs Web Design and Juwelier Schönmann 1010 Wien