wordpress snippets

Limit WordPress Search to Posts Only

Last modified: September 5, 2019

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.

Save 4 Later
Email these links to yourself