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!

  1. Jeroen

    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?

    • pippin

      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.

  2. Jeroen

    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 …

  3. pippin

    @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.

  4. Jeroen

    Should have done that first.. missed the part in the docs. Got it working now! Thanks!

  5. sstasio

    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

    • Pippin

      Change

      add_action(‘init’,'add_categories_to_cpt’);

      to

      add_action(‘init’,'add_categories_to_cpt’, 200);
  6. Aldi

    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?

    • Pippin

      That’s a completely different topic than this post.

  7. Marcello

    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’ );
    }

    • Pippin

      I’m not sure I understand your question, sorry.

  8. James Chai

    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?

    • Pippin

      “Listings” should probably be lower case: ‘listings’

  9. Archit

    Not getting where to add the code, using types plugin, but not getting where to add the desired code.
    Need help

  10. Amna Chohan

    This worked like a charm!! Thank you so much for saving my life.

  11. Imre

    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!

  12. James

    Init worked a treat! Many thanks.

  13. Flytonic

    where exactly should the code be added

Comments are closed.