How to: Exclude Pages from WordPress Search Results

Basically we don’t like display page contents in our WordPress blog search result. But by default, WordPress Search feature displays published posts and pages in search results. So how to Exclude Pages from WordPress Search Results?. In this note, I will show you how to make your search more relevant and less crowded by excluding pages from WordPress search results. It’s easy!.

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

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

Now Save your functions.php file. That’s it you’re finished.

In this script, I setted the post_type as post. So script searches only the post contents. You can also make it do the opposite by setting the post_type to pages, so it only return pages in the search result.

You can also try another very simple method. Add this code in your search form and feel the different.

<input name="post_type" type="hidden" value="post" />

Happy blogging 🙂