How to Exclude All WordPress Pages from Search Results

WordPress TipsIf you are following my WordPress related posts in this site, you should know by now that nearly a month back I wrote a post on How to Exclude Specific WordPress Pages from Search Results. This is the continuation of that post.

Today, in this post I will show you how to Exclude all the pages from WordPress search results. Trust me, this is way easier than the Previous one. If you could use the Previous code successfully, you should be done with this in less then 5 minutes.

All you have to do is to Copy and Paste the following code on your theme’s functions.php file. Update the page once you are done. Try to search certain page contents from your Search box. You should see it’s working like charm.

<?php function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post'); }
return $query; }
add_filter('pre_get_posts','SearchFilter');
?>

Creating custom theme for your very own blog or Web site is not a whole lots of a big deal. But things might get really tricky at some point if you really don’t know what exactly you are dealing with.

At the beginning when I was developing e-dirt’s theme, I used the previous code to exclude certain pages from this site’s search result. Now, I am using this very little piece of code to Exclude all the pages which is lot easier to understand and to implement.

However, if for some reason you find this code to be not working on your site, feel free to comment or ask question from the comment section below. If you like our site, show your support as well.

You can follow us on Twitter, add on your Google+ circle or Like our Facebook page to get the latest news, updates and reviews.

advertisement
Comments: