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



25 comments
This is exactly what I needed…thank you SOOO much!! :D
thank
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.
Very useful. Thanks!
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.
works perfectly. thank you.
Is there a way to separate the categories and tags for custom post types from those used for posts on the blog?
You should create custom taxonomies for custom post types, of course you can use the name “Category” or “Tag” to make them friendly.
Great! It works :) Thanks a lot!
this worked for me…anyways i need help with related post for custom post types using tags…any help will be appreciated.thank you!
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
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?
but where do I insert the mentioned code?
In
functions.phpfile of your theme.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 ;)
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_postsaction 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 ( is_search( ) || is_tax( ) ) $query['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()”
Thanks Ed, just fixed.
Im new in blogging, i dont know how to configure it….any plugin that can install easyly? Sorry…
but exactly in which file we put the above code
In your
functions.phpfile of your theme.