X

How to Add Category Selection to Custom Post Type

Snippets by IsItWP

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.

Comments   Leave a Reply

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

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

  2. Iheruamdu Calistus Eberechukwu March 4, 2020 at 9:41 am

    Extremely helpful. Thanks

  3. Doesn’t work

    1. Hi Nat,
      Be sure to change the CUSTOM_POST_TYPE to the name of the custom post type that you made.

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

    1. The register_taxonomy_for_object_type() function is used because it adds taxonomies to post types.

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

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

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

  6. Extremely helpful (in my hour of need!)
    Thanks so much for sharing 🙂

  7. Mohamed Ramadan March 19, 2012 at 5:19 am

    Thanks 

  8. Adedoyin Kassem April 11, 2011 at 3:20 pm

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

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

  9. Alexandre Klostermann April 5, 2011 at 8:25 pm

    La bamba!

    1. 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/

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!