Limit Search to Only Posts in WordPress

WordPress search gets a bad wrap, but it isn’t terrible, it’s just no Google. If you want to limit the search results page to show only posts then there’s a handy filter called pre_get_posts.

function custom_search_filter($query) {
    if ($query->is_search) {
        $query->set('post_type', 'post');
    }

    return $query;
}

add_filter('pre_get_posts','custom_search_filter');

Follow IP on Twitter Like IP on Facebook

Advertisement