RobPL
RobPL

Reputation: 61

Custom post type uses same categories as post-type instead of a unique list

I have a WordPress site where I'm using the Post Type section for it's purpose as a blog but also have a custom post type (Composers) that I want to create categories for. the code I have includes the following

            'capability_type'     => 'page',
            'has_archive'         => true,
            'hierarchical'        => false,
            'menu_position'       => null,
            'exclude_from_search' => false,         
        'show_in_rest'        => true,
            'taxonomies'          => array( 'category' ),   
        );

        register_post_type( $post_type, $args );

but what I wind up with is both this and the main Post Type using the same list of categories. To avoid confusion I need the custom post type to have a separate list. The code can create multiple post types that will be used in the future and the ability to have separate category types for all will be useful. What do I need to change or add to make this happen?

Upvotes: 0

Views: 213

Answers (2)

RobPL
RobPL

Reputation: 61

In the end I used this code

function composers_custom_category() {
$labels = array(
    'name'                       => _x('Composer Categories', 'Taxonomy 

General Name', 'twentytwentyfour'),
        'singular_name'              => _x('Composer Category', 'Taxonomy Singular Name', 'twentytwentyfour'),
        'menu_name'                  => __('Composer Categories', 'twentytwentyfour'),
        // Other labels can be set here
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true, // Like categories
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'show_in_rest'               => true,
        'rewrite'                    => array('slug' => 'composer-category'),
    );
    register_taxonomy('composer_category', array('composers'), $args);
}
add_action('init', 'composers_custom_category', 0);

// Register Custom Taxonomy for Tags
function composers_custom_tags() {
    $labels = array(
        'name'                       => _x('Composer Tags', 'Taxonomy General Name', 'twentytwentyfour'),
        'singular_name'              => _x('Composer Tag', 'Taxonomy Singular Name', 'twentytwentyfour'),
        'menu_name'                  => __('Composer Tags', 'twentytwentyfour'),
        // Other labels can be set here
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => false, // Like tags
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'show_in_rest'               => true,
        'rewrite'                    => array('slug' => 'composer-tag'),
    );
    register_taxonomy('composer_tag', array('composers'), $args);
}
add_action('init', 'composers_custom_tags', 0);

Upvotes: 0

Savan
Savan

Reputation: 11

You have to create new taxonomy by using this function register_taxonomy()

Here is docs:

https://developer.wordpress.org/reference/functions/register_taxonomy/

Then you just replace that taxonomy slug to your current taxonomies array value here:

'taxonomies' => array('unique_slug')

Upvotes: 0

Related Questions