wordpress snippets

Limit WordPress Search to Posts Only

Last modified: January 13, 2020

Cloudways

You know that weird feeling you get when you use a search form on a site and one of the results is “home” or “about”?

Well with this simple hack you can limit the search results to only show posts in the search results page.

Head over to your functions.php file, and add the following at the bottom:

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

That’s it.

Now if you use your search bar, you will only get posts in the results page.

Enjoy.

Show More

* This button will show the rest of the post and open up an offer from a vendor
Save 4 Later
Email liked links to yourself