Are you looking for a way to add category selection to custom post type? While there’s probably a plugin for this, we have created a quick code snippet that you can use to add category selection to custom post type in WordPress.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
function reg_cat() { register_taxonomy_for_object_type('category','CUSTOM_POST_TYPE'); } add_action('init', 'reg_cat');
Don’t forget to change the CUSTOM_POST_TYPE
to the name of the custom post type you created.
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 43 simple WordPress themes to start a website and how to create a guest post submission form in WordPress.
It’s been a long time but how do you implement a custom category taxonomy? I’ve created categories for my CPT but this snippet shows the regular WP categories.
Hey Mark,
Thank you for the question. After some research and tests we think this might be a better solution for you. Could you try it and let us know.
function create_custom_taxonomy() {
register_taxonomy(
'custom_category',
'your_custom_post_type',
array(
'label' => __( 'Custom Categories' ),
'rewrite' => array( 'slug' => 'custom-category' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_custom_taxonomy' );
Of course, please remember to Replace ‘your_custom_post_type’ with the CPT name.
Extremely helpful. Thanks
Doesn’t work
Hi Nat,
Be sure to change the
CUSTOM_POST_TYPE
to the name of the custom post type that you made.It would be cool if you talked about how and why you implemented this approach. As a developer, I like to understand the ‘why’ behind the solution.
The register_taxonomy_for_object_type() function is used because it adds taxonomies to post types.
Does this work for custom post types and custom taxonomies? For the default taxonomy with custom post type, it is working, but not for custom taxonomy.
Yes,
register_taxonomy_for_object_type()
should work for any registered taxonomy. If it doesn’t work, you may want to check if the taxonomy has been registered properly.Hey there, Thanks for this info. I would like to add categories to my woocommerce products to further structure data and pull products into some category based widgets (i realise products have a “product category” already) Can you tell me what the code should read for Woocommerce custom post product types. I am unsure how to find this out.
Extremely helpful (in my hour of need!)
Thanks so much for sharing 🙂
Thanks
Nice approach, but i do think instead of writing this somewhat “much” code, the theme developer should just add this line below into the register_post_type function.
‘taxonomies’ => array(‘category’)
Hi Adedoyin
Yes this is very true, this is just another method to add taxonomies however I have used this method to limit access to taxonomies editing based on role.
La bamba!
Cool glad you like the snippet, also check out add tag selection.
http://wpsnipp.com/index.php/functions-php/add-tag-selection-to-custom-post-type/