Sometimes, when using a custom post type in your plugins (or themes), it is desirable to add the already-registered taxonomies (Post Tags and Categories by default) to your custom object type. This quick code snippet will show you how to do that.
Using the register_taxonomy_for_object_type(), we will specify a taxonomy and an object type, then hook our function into an init hook.
1 2 3 4 | add_action('init','add_categories_to_cpt'); function add_categories_to_cpt(){ register_taxonomy_for_object_type('category', 'post_type_name'); } |
This will work fine, but sometimes the init hook won’t always work. If you your custom post type is created in a plugin, you may need to use the plugins_loaded hook instead, which fires after all plugins have been loaded. Like this:
1 2 3 4 | add_action('plugins_loaded','add_categories_to_cpt'); function add_categories_to_cpt(){ register_taxonomy_for_object_type('category', 'post_type_name'); } |
Enjoy!
I want to have Post Tags added to the Custom Post Types created. Which file do I need to add the code to? Is there another way to have the Post Tages enabled?
Just use this:
register_taxonomy_for_object_type('post_tag', 'post_type_name');
Put it in your functions.php, or plugin file if you’re writing a plugin.
Hmm.. don’t get it … I’m using the Easy Content Types plugin and want to have the post tags available in the created Content Types. Which functions.php do you mean, from the theme, wordpress or …
@Jeroen – Ah, you should have said so 😉 There’s a section in the documentation that says how to do it. Go to the help page and look in the taxonomy section.
In the future when asking about a plugin, please comment on the plugin post.
Should have done that first.. missed the part in the docs. Got it working now! Thanks!
Pippin,
I’ve been trying to add the categories to your sugar calendar plugin using this code, but nothing seems to happen. I’ve tried it both ways. Here is the code I’ve tried:
‘
//Register categories with events
add_action(‘init’,’add_categories_to_cpt’);
function add_categories_to_cpt(){
register_taxonomy_for_object_type(‘category’, ‘sc_event’);
}
‘
and
‘
//Register categories with events
add_action(‘plugins_loaded’,’add_categories_to_cpt’);
function add_categories_to_cpt(){
register_taxonomy_for_object_type(‘category’, ‘sc_event’);
}
‘
and yes, I put these in the funtions file.
Sean in SF
Change
to
Thanks for the tips! But how can I tell if I had successfully add a custom taxonomy to a custom post type with this method? Is there any way to check the taxonomy’s relationship with the post type?
That’s a completely different topic than this post.
Theres a way to use only some categories to the CUSTOM POST TYPE? For example:
i have 6 categories on my default post type. and i want to use 2 of them for my CUSTOM POST TYPE.
‘Till now i was able to use all categories including this to the finction.php:
add_action( ‘init’, ‘wpse6098_init’, 100 ); // 100 so the post type has been registered
function wpse6098_init()
{
register_taxonomy_for_object_type( ‘category’, ‘registro’ );
}
I’m not sure I understand your question, sorry.
I tried using the method mentioned above and am using the ‘Agentpress Listing’ plugin.
I inserted the following code into my functions.php file:
//* Add category to listing post type
add_action(‘init’,’add_categories_to_cpt’);
function add_categories_to_cpt(){
register_taxonomy_for_object_type(‘category’, ‘Listings’);
}
am I missing something?
“Listings” should probably be lower case: ‘listings’
Not getting where to add the code, using types plugin, but not getting where to add the desired code.
Need help
It’s best to create a custom plugin and put it there. Pluginception will make it easy to create a plugin: https://wordpress.org/plugins/pluginception/
Pingback: How to Add Existing Taxonomies to WordPress Custom Post Types | WPShout
Pingback: How to Add Existing Taxonomies to WordPress Custom Post Types | WP Dragons
This worked like a charm!! Thank you so much for saving my life.
I still had issues using this with custom taxonomies (of a CPT) even if I changed the init.
what worked at the end is changing the action priority like this
add_action(‘init’,’add_categories_to_cpt’,999);
thanks for the useful tip!
Init worked a treat! Many thanks.
where exactly should the code be added