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(
        '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.


  1. Great, very helpful! Thanks!

  2. it’s possible remove slug? My structure is /%postname%/

  3. thanks for the tutorial. just fixed a site..!

  4. Oliver Nielsen

    April 29, 2014 at 8:15 PM

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


  5. Iftekhar Bhuiyan

    April 16, 2014 at 11:31 AM

    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.

    • rilwis

      April 17, 2014 at 4:53 PM

      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.

      • Iftekhar Bhuiyan

        April 18, 2014 at 3:39 AM

        Thank you very much for your prompt reply. What you just said, does make sense for sure and I agree with you. Thank you once again.

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

  7. Thanks you!!!

  8. Usefull post! Tanks a lot!

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

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

  11. Thanks, Great Post Buddy..

  12. Great post. Thanks for sharing!

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

  14. Thanks the tags are working…

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

  15. 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!

    • rilwis

      August 2, 2012 at 6:46 PM

      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.

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

    • rilwis

      July 18, 2012 at 10:48 PM

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

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

  18. Simon Stafferton

    May 10, 2012 at 9:33 PM

    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!

  19. 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!

  20. Dorian Speed

    May 2, 2012 at 6:50 PM

    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.

  21. Thanks guy, i am looking for this and you help me a lot!

  22. Matthew Boynes

    April 18, 2012 at 1:03 AM

    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;

  23. Thank you, very helpful for me.

    i can make category & tag in my custom page.

    thank you.

  24. Great tips. Does it work in Thesis?

  25. 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(
    ‘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’);

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

    Salman Sohail

  27. 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!

    • rilwis

      February 22, 2012 at 1:50 PM

      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.

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

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

    • rilwis

      February 16, 2012 at 1:37 PM

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

  30. Tnx man….you save me a lot of time…all the best

  31. but exactly in which file we put the above code

  32. Im new in blogging, i dont know how to configure it….any plugin that can install easyly? Sorry…

  33. 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 ;)

    • rilwis

      December 1, 2011 at 3:32 AM

      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' ) );
      • Where do I add this? To the archive page or functions?

      • This Breaks the site when I add it to functions.php

      • Yeah this just breaks my site too. Any suggestions?

        • is “is_tax()” not “is_ta()”

        • rilwis

          January 31, 2012 at 3:59 AM

          Thanks Ed, just fixed.

        • 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!

        • Tran Ngoc Tuan Anh

          April 18, 2012 at 1:38 AM

          Just fixed the code.

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

      • Great Thanks!!!
        I’m happy that found this page))

  34. but where do I insert the mentioned code?

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



    • rilwis

      November 18, 2011 at 1:47 AM

      Maybe you should flush the rewrite rule by going to Settings -> Permalinks.

      • I had the same problem, flushed the rewrite rule in permalinks, but nothing.

        Any clue on what to do?

        • 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

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

  37. Great! It works :) Thanks a lot!

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

    • rilwis

      October 18, 2011 at 3:12 AM

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

  39. works perfectly. thank you.

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

  41. Very useful. Thanks!

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

  43.  This is exactly what I needed…thank you SOOO much!!  :D

Leave a Reply