Categories assigned to custom post type not found

the default search function will only look for posts and pages so you need to add your post_type to the search and can do this in two ways

using the ‘pre_get_posts’ filter to add your post type before the query like this:

function Search_CPT_Filter($query) {
    $post_type = $_GET['type'];
    if (!$post_type) {
        $post_type = array('post','post type name');
    }
    if ($query->is_search) {
        $query->set('post_type', $post_type);
    };
    return $query;
};

add_filter('pre_get_posts','Search_CPT_Filter');

or, adding the post type to the search form in an hidden filed like this:

<input type="hidden" name="post_type" value="post type name" /> 

and in both cases just change ‘post type name’ to the name of your post type.

Leave a Comment