Adding Categories And Post Tags Boxes For Custom Post Type In WordPress

custom post type with category tags Adding Categories And Post Tags Boxes For Custom Post Type In WordPress

Categories and Post Tags boxes are built-in meta boxes that have been already registered in WordPress. We can use them not only for default posts or pages but also for custom post types as well in WordPress. And this can be done easily with argument taxonomies of register_post_type() function when registering new custom post type.

In this example, I’ll register a new post type called “demo” with categories and post tags boxes:

add_action('init', 'demo_register_post_type');

function demo_register_post_type() {
    register_post_type('demo', array(
        'labels' => array(
            'name' => 'Demos',
            'singular_name' => 'Demo',
            'add_new' => 'Add new demo',
            'edit_item' => 'Edit demo',
            'new_item' => 'New demo',
            'view_item' => 'View demo',
            'search_items' => 'Search demos',
            'not_found' => 'No demos found',
            'not_found_in_trash' => 'No demos found in Trash'
        ),
        'public' => true,
        'supports' => array(
            'title',
            'excerpt'
        ),
        'taxonomies' => array('category', 'post_tag') // this is IMPORTANT
    ));
}

The taxonomies is an array of registered taxonomies that will be used with this post type. The taxonomy for categories is category and for post tags is post_tag. Both of them are automatically registered, and we can use them without re-defining.

Sometimes, your blog has registered a custom post type already, and you don’t want to search in a bunch of code just to find out where the register_post_type() function to add a small line for categories and post tags boxes. To solve this problem, you can add them (categories and post tags boxes) later using register_taxonomy_for_object_type() function like this:

add_action('init', 'demo_add_default_boxes');

function demo_add_default_boxes() {
    register_taxonomy_for_object_type('category', 'demo');
    register_taxonomy_for_object_type('post_tag', 'demo');
}

Note that all these functions have to be called with init action hook. Otherwise, the custom post type is still registered, but no categories or post tags boxes.

74 Comments

  1. Thanks for sharing. Just what I was looking for. Hadn’t expected it to be this easy (after having looked in WordPress Codex first)

    :)

    Reply
  2. Hello Tran,
    Thank you very much for your post. I am well aware of the fact that we can use default “post_tag” taxonomy with CPTs or simply we can create another custom taxonomy for the same purpose. My question is, what would be the best practice?

    I have custom CPTs like tutorials, portfolio, themes etc. Should I use the default post_tag taxonomy for adding tags on posts or to create a new one? Thank you in advance.

    Reply
    • IMHO, using default ‘post_tag’ for other CPTs is not a good idea. Think about situation when you click on a tag? What do you expect? List of posts or mixed of posts and ‘portfolios’?

      I always recommend using different taxonomies for different CPTs.

      Reply
  3. Hi, I have managed to add the custom post type to the admin, make a new post and that shows fine on the front end. Categories and tags show in both front and back end. However on the front end, if i click on a category, the post doesnt show up on the archive page. Any help????
    I would really like to move all my current posts across to a new post type but without this working it makes things very difficult for me.
    Thanks!!

    Reply
  4. Thanks for the tip, I find it very useful. Your work will appear on several websites that I’m currently working on, and you don’t even know it :-) That’s the beauty of writting nice little tutorials – making the World Wide Web a better place.

    Reply
  5. Hi,

    I have a custom post type called Recipes that was coded into a theme I bought. What I would like to do is add categories to the custom post type since it already has tags. Could you help? Any help is welcomed.

    Reply
    • After some searching I found where I had to place the ‘category’, taxonomy.

      Thank you :-)

      Reply
  6. Extremely helpful, thanks so much for sharing.
    This really opens up the custom post types functionality and makes it much more useful for me.
    Awesome!!

    Reply
  7. Thanks the tags are working…

    BUT if I click on the ” Choose from the most used tags ”
    I get ” No tags found! “

    Reply
    • @Marc – You have no tags saved. Add tags and update, then click that link again. Your most-used tags will appear.

      Reply
  8. Very helpful post. I also wanted to say that I recognize the Genesis theme. It’s funny that I saw this site because I’ve been debating on which framework to buy, or even go with a free one like thematic. I’ve been using WP for a couple of years but have recently started to learn a little about theme development. Would you recommend Genesis? This is the exact theme that I was wanting to start with too.

    Thanks again for the post!

    Reply
    • I recommend Genesis for SEO benefits. It’s the strongest thing of Genesis, which I think it does SEO best! Other things like code structure, modules, easy to extend, etc. I think every frameworks have.

      Reply
  9. Hi,
    first of all great snippet, but i have aquestion
    when you add the categories to the custom post type demo is there any way to add the categories but excluding some of them (instead of being all added)?

    add_action(‘init’, ‘demo_add_default_boxes’);

    function demo_add_default_boxes() {
    register_taxonomy_for_object_type(‘category’, ‘demo’);
    register_taxonomy_for_object_type(‘post_tag’, ‘demo’);
    }

    thanks in advanced

    Reply
    • AFAIK, we don’t have any way to do that unless you use a custom taxonomy for ‘demo’ post type.

      Reply
  10. My theme was missing tags for custom post types. Many thanks to you, I fixed it following your instruction.

    Reply
  11. Awesome – this is so simple and worked perfectly for me, trying to get another plugin (Geo-mashup) to work with custom post types. All other possible solutions looked massively involved, this fixed things with 3 lines of code. Many thanks!

    Reply
  12. Thanks a lot, i was stuck on this for a day. By the way is thr a way to display a subset of categories (limited by a parent category key) in this category list for a custom post type. tx again!

    Reply
  13. Perfect! This is just what I needed to know. I found your site via the WordPress experts group on LinkedIn and I appreciate all of the helpful content you’re providing.

    Reply
  14. FGreat post, thanks for sharing. From the comments, for `rw_add_custom_post_types`, that should actually be:
    function rw_add_custom_post_types( $query ) {
    if ( $query->is_tag )
    $query->set('post_type', array('post', 'movies', 'actors'));
    return $query;
    }

    Reply
  15. Thank you, very helpful for me.

    i can make category & tag in my custom page.

    thank you.

    Reply
  16. Hi and thanks a lot for this great tutorial. I have the same problem as some other people here though. I’ve created a custom post type for quizzes on my website but I can’t seem to get it to show. I’m using the theme Sight, where all the categories are shown in the header. I want one of those categories to show all my quizzes.

    This is the code I’ve got in my functions.php right now:

    add_action(‘init’, ‘create_quizzes’);

    function create_quizzes() {
    register_post_type(‘quiz’, array(
    ‘labels’ => array(
    ‘name’ => ‘Quizzes’,
    ‘singular_name’ => ‘Quiz’,
    ‘add_new’ => ‘Lägg till quiz’,
    ‘edit_item’ => ‘Redigera quiz’,
    ‘new_item’ => ‘Nytt quiz’,
    ‘view_item’ => ‘View quiz’,
    ‘search_items’ => ‘Search quizzes’,
    ‘not_found’ => ‘No quizzes found’,
    ‘not_found_in_trash’ => ‘No quizzes found in Trash’
    ),
    ‘public’ => true,
    ‘supports’ => array(
    ‘title’,
    ‘editor’,
    ‘custom-fields’,
    ‘revisions’,
    ‘excerpt’
    ),
    ‘taxonomies’ => array(‘category’, ‘post_tag’) // this is IMPORTANT
    ));
    }

    add_action(‘init’, ‘quiz_add_default_boxes’);

    function quiz_add_default_boxes() {
    register_taxonomy_for_object_type(‘category’, ‘quiz’);
    register_taxonomy_for_object_type(‘post_tag’, ‘quiz’);
    }

    add_action( ‘pre_get_posts’, ‘rw_add_custom_post_types’ );

    function rw_add_custom_post_types( $query ) {
    if ( is_search( ) || is_tax( ) )
    $query['quiz'] = array(‘quiz’, ‘movies’, ‘actors’);
    }

    Reply
  17. Thank you so much for this great information. was looking it and ws not able to find it. thanks to your blog.

    Regards,
    Salman Sohail

    Reply
  18. Great stuff. Exactly what I was looking for but Im using Marketpress (which uses custom posts for products) and Ive implemented your code above so I can assign products to standard wordpress categories & tags. I can see the category & tag boxes but when I do a search or click on tag cloud in frontend I cannot see my products in the results. I also tried adding the pre_get_posts code to functions but no good. Help!

    Reply
    • By default, WP uses only ‘post’ post type for search page and category and tag page. Using ‘pre_get_posts’ hook is right solution. I guess the problem is how you apply hook for ‘pre_get_posts’. If you can post your code here, then I can help you.

      Reply
  19. I have read this post with great interest. However, I can not seem to show Custom Post Types that contain the same tags/categories as normal posts on the same page.

    I have added the code, as mentioned above eg pre_get_posts, changed the permalinks, saved changes but still cannot see my custom posts listed with standard posts with the same tags. I have also noted the following error when using search – ‘Cannot use object of type WP_Query as array in … functions.php line…’

    Can anyone shed any light on what my problem could be. I am trying this on WP 3.3.1 with Twenty Eleven Theme and no plugins.

    Reply
    • I guess your problem is caused by the way you show CPT. Can you post your code on pastebin and share a link here?

      Reply
      • i have the same problem with a child theme of twentyten. any help would be most appreciated.

        Reply
  20. Thank you!! This works but my custom post is limited to title and excerpt. How do I add html/visual editor and featured image to the post type? Thank you so much.

    Reply
    • You should add an attribute ‘support’ => array( ‘title’, ‘editor’, … ) when you register the custom post type.

      Reply
  21. i spent hours and hours trying to figure this out! thanks so much!
    i just have one question, do they have the same functionality? ive set several tags, but if i do a search the related posts dont appear, neighter if i press one of the tags in the tag cloud..

    just wondering whats missing now.. if you have any clues i would be really thankfull, again ;)

    Reply
    • Hi emiliano, the “tags” and “cateogries” of course have the same functionality. But by default WP don’t include custom post types in search result or archive page. That’s why you don’t see your posts with custom post type there. To make this done, you should hook into pre_get_posts action and add your custom post type, like this:

      add_action( 'pre_get_posts', 'rw_add_custom_post_types' );
      
      function rw_add_custom_post_types( $query ) {
          if ( $query->is_search || $query->is_tag ) 
             $query->set('post_type', array( 'post', 'movies', 'actors' ) );
      }
      
      Reply
        • this isn’t working for me. I added that code under the code in your post and all it does it break search.

          So while the custom post type stuff all works, it doesn’t display anywhere on my blog regardless of choosing a category or tag. Help!

        • Ah, nice, this answered my question above. Note that if you want them to show up on category archive pages as well, you need to add something extra Here it all is:

          add_action( ‘pre_get_posts’, ‘cpt_add_custom_post_types’ );

          function cpt_add_custom_post_types( $query ) {
          if ( $query->is_search || $query->is_tag || $query->is_category )
          $query->set(‘post_type’, array( ‘post’, ‘cpt_images’) );
          }

  22. Hi there,

    This page has certainly clarified somethings but I have been struggling all afternoon with my cpt’s. I used a ‘custom post type UI’ plugin but when I categorise my cpt’s I get a return of ‘nothing found so I cut and pasted your code without changing anything to see would this solve things.

    I get a ‘demos’ custom type with categories & tags (all good). I make a new demo and categorise it and publish. All fine, but when I click on the category under which the demo was included and I still get a return of ‘nothing found’. Any ideas what is going wrong. I’m just using WP twenty eleven default theme.

    Stumped!

    Paul

    Reply
        • I’m also having this issue, where Custom Post Types do not show up on Category Archive pages…normal posts in a particular category are showing, but no custom post types do. Any ideas?

        • This is probably very very old, but did you manage to get custom post types to show in the category archives?? Im really struggling to get this to work

  23. this worked for me…anyways i need help with related post for custom post types using tags…any help will be appreciated.thank you!

    Reply
  24. Is there a way to separate the categories and tags for custom post types from those used for posts on the blog?

    Reply
    • You should create custom taxonomies for custom post types, of course you can use the name “Category” or “Tag” to make them friendly.

      Reply
  25. Thanks for the great tutorial, previously I used a plugin to do that and now hopefully I managed to integrate directly into the theme files.

    Reply
  26. thanks! I was looking at much more complex solutions to put the default category menu on my custom post but this was so much easier. I like the 2nd suggestion if you don’t know the post or when it was registered, just tack it to the bottom to have it add it.

    Reply

Leave a Reply