Categories assigned to custom post type not found

Here’s my issue:

I have assigned a custom post type to certain categories. When I search for these categories, either through search or entering the url directly. However, if I assign a normal post type to a category, it becomes searchable and can be entered by the url directly. I thought I got everything in my custom post type code, but I must have missed something. Any help would be appreciated:

add_action('init', 'create_trainingplan_post_type');

function create_trainingplan_post_type() {
register_post_type('trainingplan', array(
    'labels' => array(
        'name' => 'Training Plans',
        'singular_name' => 'Training Plan',
        'add_new' => 'Add New Training Plan',
    'add_new_item' => 'Add New Training Plan',
    'edit' => 'Edit Training Plans',
        'edit_item' => 'Edit Training Plan',
        'new_item' => 'New Training Plan',
        'view_item' => 'View Training Plans',
        'search_items' => 'Search Training Plans',
        'not_found' => 'No Training Plans found',
        'not_found_in_trash' => 'No Training Plans found in Trash'
    ),
    'public' => true,
'exclude_from_search'=> false,
'rewrite' => array('slug' => 'my-custom-training-plan', 'with_front' => true),
'supports' => array(
    'title',
    'excerpt',
    'editor',
    'custom-fields',
    'revisions',
    'thumbnail',
    'author',
    'page-attributes'

    ),
    'taxonomies' => array('category', 'post_tag') // this is IMPORTANT
));
flush_rewrite_rules();
}

Thank you for the help so far, I’ve added comments from the answer I received so far to make it more clear in my question:

@ בניית אתרים I can actually search for the custom post type and find it. It’s that the categories that are assigned only to custom post types cannot be searched or found. Basically, I am trying to enable a user to search by category, but categories that are assigned to only a custom post type are not appearing. For that matter, even if I type in the direct url of the category, it takes me to an error page: “Sorry, we can’t find the category you’re looking for at this URL. Please try selecting a menu item from above or to the side of this message to get where you…”

@ בניית אתרים I assigned the categories to the custom post type like you would a normal post. While writing the post, I ticked the categories I wanted included. When in the edit posts dashboard, it does confirm that categories are assigned to the cpt. However, when I go to the category url or search for posts within that category, cpt do not show.

Thanks again

1 Answer
1

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