Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two custom post type sharing same category

Tags:

wordpress

Is it possible to add same category for multiple custom post type?

for example,

coupon post type - deal post type - both should share common cateogires, such as health, travel etc.

But, when we create new post type, looks like, we need to provide custom category also.

Do you have any clue how to create custom post types without custom categories?

like image 893
stackguy Avatar asked Mar 31 '16 22:03

stackguy


1 Answers

Yes, custom-post-types can have multiple taxonomies ( categories, tags, custom ).

Yes, you can have a custom post type without a taxonomy.

Adding categories to custom post types is done like so:

'taxonomies'          => [ 'category' ], // <--- add this (or use 'post_tag' to add tags to the CPT)
'public'              => true,
'show_ui'             => true,
'exclude_from_search' => true,
'hierarchical'        => true,
'supports'            => [
    'title', 
    'editor', 
    'thumbnail',
],
'query_var'           => true,

If you want to add a custom taxonomy to multiple post types, you can do this:

function people_init() {
    // create a new taxonomy
    register_taxonomy(
        'people', [ // <-- 'people' taxo added to posts, pages, & custom_post_type
            'post', 
            'page', 
            'custom_post_type',
        ],
        array(
            'label'        => __( 'People' ),
            'rewrite'      => [
                'slug'     => 'person',
            ],
            'capabilities' => [
                'assign_terms' => 'edit_guides',
                'edit_terms'   => 'publish_guides',
            ]
        )
    );
}
add_action( 'init', 'people_init' );
like image 88
admcfajn Avatar answered Sep 16 '22 22:09

admcfajn