Divi Page Builder Black Friday Crazy Deal!  GO!
wordpress snippets

Limit WordPress Search to Posts Only

Last modified: January 13, 2020

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;

That’s it.

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


Save 4 Later
Email these links to yourself