0

I have created a custom post type 'Gallery' and assign Default 'category' of post taxonomy to this custom post type.

register_post_type( 
        'gallery', 
        array(
            'labels' => array(
                'name' => 'Galleries',
                'singular_name' => 'Gallery',
                'add_new' => 'Add New Image Gallery',
                'add_new_item' => "Add New Gallery",
                'edit_item' => "Edit Gallery",
                'new_item' => "New Gallery",
                'view_item' => "View Gallery",
                'view_items' => "View Galleries",
                'search_items' => "Search Galleries",
                'not_found' => "No galleries found",
                'not_found_in_trash' => "No galleries found in trash",
                'all_items' => "All Image Galleries",
                'insert_into_item' => "Insert into Gallery",
                'uploaded_to_this_item' => "Uploaded to this Gallery",
            ),
            'public' => true,
            'taxonomies' => array('category'),
            'hierarchical' => true, 
            'publicly_queryable' => true,
            'has_archive'   => true,
            'show_ui' => true,
            'has_archive' => true,
            'query_var' => true,
            'supports' => array('title', 'author','thumbnail','editor'),
            'rewrite' => true,
            'menu_icon' => 'dashicons-images-alt2',
            'register_meta_box_cb' => array( $this, 'gallery_meta_boxes' )
        )
    );

    //register_taxonomy_for_object_type( 'category', 'gallery' );

In backend add/edit Gallery post, its giving an option to to add/select category for that custom post but in frontend category page, this post is not displaying. Please suggest

Thanks

1

The default query doesn't include custom post types. You have to use pre_get_posts hook to modify your query. Hope this solves your issue

function custom_posttype_query( $query ) {
    if( (is_category() || is_tag()) && $query->is_archive() && empty( 
        $query->query_vars['suppress_filters'] ) ) {
        $query->set( 'post_type', array(
           'post', 'gallery'
        ));
    }
    return $query;
}
add_filter( 'pre_get_posts', 'custom_posttype_query' );
  • Thanks for replay, yes its working but i added some extra condition for that post also who is using post category as taxonomy, so it will not affect that post also. – Meenakshi Khandelwal Nov 7 '17 at 14:33
  • Can't understand what you wanna achieve...you want other post type added as well?? – Piyush Rawat Nov 7 '17 at 14:39
  • Thanks, i got my answer, i just added some extra condition for other custom post who is using same post category, so this will not replace theme. Thanks for help – Meenakshi Khandelwal Nov 7 '17 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.