0

Is it possible to reuse the "category" slug for a custom post type and still have the categories editable within the CPT menu?

With a CPT of "work" I'd like to be able to have URLS like this domain.com/work/category

I've tried this code but the taxonomy just doesn't show up in the CPT menu:

$args = array(
    'labels' => array(
        'name'                  => __( 'Work Projects' ),
        'singular_name' => __( 'Work Project' ),
        'add_new_item'  => __( 'Add New Work Project' ),
        'edit_item'         => __( 'Edit Work Project' )
    ),
    'public'                => true,
  'has_archive'         => true,
  'publicly queryable'  => true,
  'menu_icon'                       => 'dashicons-lightbulb',
  'show_in_nav_menus'       => true
);
register_post_type( 'work', $args );

$args = array(  
  'hierarchical' => true,  
  'labels' => array('name' => 'Work Categories', 'singular_name' => 'Work Category'), 
  'query_var' => true,
  'show_ui' => true,
  'show_in_menu' => true,
  'rewrite' => array(
      'slug' => 'type', 
      'with_front' => true 
  )
);  
register_taxonomy( 'category', 'work', $args );

1 Answer 1

0

To share a taxonomy between multiple post types when you register her, you have to pass an array with the slug if all posts types that she will belong to.

Example:

register_taxonomy( 'category', array( 'work', 'another_cpt' ), $args );

But in your case the category taxonomy is already registered, so you have to do the following:

function add_category_to_work_cpt(){
    register_taxonomy_for_object_type( 'category', 'work' );
}
add_action( 'init', 'add_category_to_work_cpt' );

See the Codex and this answer for reference.

4
  • That's brilliant thank you! What if I wanted to rename the default posts category to "posts-cat" so I can keep "category" for the cpt?
    – David
    Jul 3, 2017 at 17:01
  • The above answer worked? if yes can you please mark as the correct? it will help people with the same problem, thanks! Jul 3, 2017 at 17:03
  • It will be easier if you just register a new taxonomy for the cpt Jul 3, 2017 at 17:13
  • Yes that would indeed be ideal however the client just wants to use "category" in the URL. Is it possible? If not then they'll have to have a different slug or share the default categories as per your answer.
    – David
    Jul 3, 2017 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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