How To Limit Search Results to Specific Post Types in WordPress

WordPress comes with a built-in search feature which allows you search for keywords within your post title and post content.

Quick Tip : Disable Search in WordPress

In the previous posts, we have already seen how we can Change Search Permalink in WordPress. In this post, we are going to limit the search results to a specific post type.

Copy the following code and paste it inside your functions.php file located inside your theme directory.

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

The above code will limit the search results to post only. You can change it your own custom post type by replacing it with post. 

0 Comments

Leave a Comment.