14

All, I'm creating my own post type using the following code:

add_action( 'init', 'create_team_post_type' );
function create_team_post_type() {
  register_post_type( 'team',
    array(
      'labels' => array(
        'name' => __( 'Teams' ),
        'singular_name' => __( 'Team' )
      ),
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'taxonomies' => array('category'),
        'supports' => array('title','editor','thumbnail')
    )
  );
}

In this I'm allowing a category. When I click on the category for this I see the categories already listed from my post categories. I'd like to only show the categories for this custom post type. In addition when I add a new category to the custom post type I only want it to apply to this custom post type. How do I go about doing this?

17

I would say that you need to also create a custom taxonomy if you want it to be limited to the one post type. "Categories" is already connected to posts by default.

From the WordPress Codex

function people_init() {
    // create a new taxonomy
    register_taxonomy(
        'people',
        'post',
        array(
            'label' => __( 'People' ),
            'rewrite' => array( 'slug' => 'person' ),
            'capabilities' => array(
                'assign_terms' => 'edit_guides',
                'edit_terms' => 'publish_guides'
            )
        )
    );
}
add_action( 'init', 'people_init' );

So, if you called it "team-category", you would then use that in the 'taxonomies' array in your post type.

Here's a more specific example:

function tr_create_my_taxonomy() {

    register_taxonomy(
        'team-category',
        'team',
        array(
            'label' => __( 'Category' ),
            'rewrite' => array( 'slug' => 'team-category' ),
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'tr_create_my_taxonomy' );
| improve this answer | |
  • This looks good. How do I display my custom taxonomy in the custom post? I tried to change 'taxonomies' => array('category') to 'taxonomies' => array('people') but it doesn't show those categories. – user1048676 Nov 16 '14 at 20:20
  • Make sure to change 'post' to 'team' in the example code if that's where you want it to show up. – SkyShab Nov 16 '14 at 20:24
  • I guess I should be more clear. It shows up there but when I'm adding a new post in the custom post type I'd like it to show up as checkboxes like on the standard post type. Right now it just shows me a box like the tags instead of the custom categories. – user1048676 Nov 16 '14 at 20:28
  • 2
    If you want it 'category style' then set hierarchical to true. The default, false, displays it 'tag style'. I added the former to my latest code example. – SkyShab Nov 16 '14 at 20:37
  • To display the custom taxonomy in the Gutenberg page edit, add 'show_in_rest' => true to the register_taxonomy options. – Fabian von Ellerts Nov 15 '19 at 10:45

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.