Are you looking for a way to add categories to a custom post type easily?
Categories are one of the built-in taxonomies in WordPress, but they only appear in posts by default.
In this article, we’ll show you how to add categories to a custom post type in WordPress.
When You May Need to Add Categories to a Custom Post Type?
WordPress allows plugin developers and website owners to create their own content types. Similar to posts and pages, these custom post types can also have their own taxonomies like categories and tags.
For instance, if you publish movie reviews, then you may want to create a custom post type for ‘Movies’.
You may also need to organize your movies in proper topics, for that you’ll need a custom taxonomy. You can create a taxonomy called genre.
However, what if you wanted to use the same categories you use for your blog posts with the Movies post type?
This is when you need to enable categories for your custom post type. This would allow you to use the same categories for both your blog posts as well as your custom post type.
That being said, now let’s take a look at how to easily add categories to a custom post type in WordPress.
Add Categories to a Custom Post Type with a Plugin
If you’re a beginner, we recommend using the Custom Post Type UI plugin to create custom post types.
With the Custom Post Type UI plugin, you have the option to associate your custom post type with any built-in or custom taxonomy, including categories.
First, you need to install and activate the Custom Post Type UI plugin. For more details, see our step by step guide on how to install a WordPress plugin.
Upon installation, you need to visit CPT UI » Add/Edit Post Types to create a new custom post type or edit an existing custom post type you created with the plugin.
Scroll down to the bottom where the Settings Options are. From there, you will see the Taxnomies area.
You need to check the box next to categories and save your custom post type.
Don’t forget to click on the ‘Save Post Type’ button to store your settings.
Now, you can edit any content under that particular post type, and you’ll see the option to select categories in the right column in the WordPress block editor.
Manually Adding Categories to a Custom Post Type with Code
If you created your custom post type by adding the code in your theme’s functions.php file, a site-specific plugin, or in a code snippets plugin, then you will have to modify the code to add category as supported taxonomy.
All you need to do is add this line in the arguments for your custom post type.
'taxonomies' => array( 'category' ),
It is likely that you may already have this line in the existing code for your custom post type with some other custom taxonomy in it. If you do, then you just need to add a comma after that and add a category, like this:
'taxonomies' => array('topics', 'category' ),
Here is a full example of code where we have created a custom post type called ‘Movies’ with support for built-in categories.
function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => __( 'Movies', 'twentythirteen' ),
'parent_item_colon' => __( 'Parent Movie', 'twentythirteen' ),
'all_items' => __( 'All Movies', 'twentythirteen' ),
'view_item' => __( 'View Movie', 'twentythirteen' ),
'add_new_item' => __( 'Add New Movie', 'twentythirteen' ),
'add_new' => __( 'Add New', 'twentythirteen' ),
'edit_item' => __( 'Edit Movie', 'twentythirteen' ),
'update_item' => __( 'Update Movie', 'twentythirteen' ),
'search_items' => __( 'Search Movie', 'twentythirteen' ),
'not_found' => __( 'Not Found', 'twentythirteen' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentythirteen' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'movies', 'twentythirteen' ),
'description' => __( 'Movie news and reviews', 'twentythirteen' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'show_in_rest' => true,
// This is where we add taxonomies to our CPT
'taxonomies' => array( 'category' ),
);
// Registering your Custom Post Type
register_post_type( 'movies', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'custom_post_type', 0 );
Displaying Multiple Post Types on Category Page
By default, the category pages on your WordPress site will only display the default ‘Posts’ post type.
To display your custom post types on the same category page as your default posts, you need to add this code into your theme’s functions.php file or in a code snippets plugin:
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if( is_category() ) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array('nav_menu_item', 'post', 'movies'); // don't forget nav_menu_item to allow menus to work!
$query->set('post_type',$post_type);
return $query;
}
}
Note: Don’t forget to replace ‘movies’ with the name of your own custom post type.
We recommend adding this code using WPCode, the best code snippets plugin on the market. WPCode makes it safe and easy to add custom code in WordPress without editing your theme’s functions.php file.
To get started, you need to install and activate the free WPCode plugin. If you need help, see our tutorial on how to install a WordPress plugin.
Once the plugin is activated, go to Code Snippets » Add Snippet from the WordPress dashboard.
From the Add Snippet page, hover your mouse over the ‘Add Your Custom Code (New Snippet)’ option and click the ‘Use snippet’ button.
Next, add a title for your snippet at the top of the page, which is just for your reference and can be anything to help you remember what the code is for.
Then, paste the code from above into the ‘Code Preview’ box and select ‘PHP Snippet’ as the code type from the dropdown menu on the right.
After that, simply toggle the switch at the top of the page from ‘Inactive’ to ‘Active’ and click the ‘Save Snippet’ button.
You can now visit a category archive page and it will display your entries from your custom post type.
You can use the same methods to add tags to your custom post types as well. See our guide on categories vs. tags to learn more.
Expert Guides on Categories in WordPress
Want to learn more about using categories in WordPress? Check out these tutorials:
- How to Change the Category Order in WordPress
- How to Enforce One Category Per Post in WordPress
- How to Add Taxonomy Images (Category Icons) in WordPress
- How to Convert WordPress Categories to Custom Taxonomies
- How to Track WordPress Category and Tag Analytics (Easy Way)
We hope this article helped you learn how to add categories to your custom post type in WordPress. You may also want to see our expert pick of the must have WordPress plugins to grow your website or these tips on how to increase your blog traffic.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Chandu Mullangi
Hi tanks for this article
To Displaying Multiple Post Types on Category Page code is working well.
I want to display multiple post types on Tags page, Can you please share that code.
WPBeginner Support
To have tags added to your custom post types you would add ‘post_tag’ to your taxonomy array so it would look like:
‘taxonomies’ => array( ‘category’, ‘post_tag’ ),
Admin
Muhamad Shahzad
How can we separate the category lists custom post type from the blog post categories. i added above code and it works. But the issue is categories of this custom post type and blog posts are mixed. they should be separated.
WPBeginner Support
The goal of this tutorial is to have them mix, for what you are looking for it may be best to use a different category for the custom post types for them to not appear in your category archive pages.
Admin
Poulomi Basu
Hello,
I used this code and it worked perfectly! The CPT shows in the post category that I chose for it.
But it doesn’t show in my blog archive. Like the default archive, which shows all posts.
How can I show it there as well?
Thanks a lot!
WPBeginner Support
You would want to take a look at our article below that covers how to do this
https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/
Admin
Connie
Please tell me how to add a CPT-category to a menu?
I have the CPT “article” with category “car” and “truck”
I want to have a custom menu with these entries:
All – cars – trucks
I search and search and don’t find it. I see the categories in the menu-administration and I can add them to the menue, but the result of these links = blank pages
I suppose that the listed categories are meant as post-categpories and not as cpt-categpories…
Do you have a hint for me?
Connie
WPBeginner Support
You may want to check your screen options in the menu area to see if the custom post type is an option there for you to add that is unchecked currently
Admin
Vico
I’ve created a post type called products and use core wp categories,
My product url structure is mysite.com/product/skf bearing 12
My category pages show mysite.com/category/bearings
Why product url doesn’t display the Category inside the Url? Like this
mysite.com/bearings/skf bearing 12
My custom post permalink is %category%%post%
I saved the permalink but still showing the same structure
dharamjeet
hey! my default posts category list shows in custom post type category list
please help me to correct it
carlos ballin
finally works for me thank you
Moises
Why did u add the twentythirteen in the code?
WPBeginner Support
Hi Moises,
This string is used for translation. If your theme supports translation, then you need to replace it with your theme’s text domain. If you are unsure about what to use here, then ask your theme support. You can also paste it as is and those strings will be ignored.
Admin
Brian
FYI, this will break your bulk edit in the admin. I added this to a bunch of post types and when I go to bulk edit, the post_type part of the query becomes “Array” instead of the post type its trying to edit in the admin.
To get around this you just need to add this before if( is_category() ):
`if ( is_admin() ) return;`
rose
Hi, I need some help.
I have 2 CPT and I don’t want them to have a shared taxonomy.
Like
CPT 1
– Taxonomy (Types)
– Type A
– Type B
CPT 2
– Taxonomy (Types)
– Type c
– Type D
Note: I dont want to show here the types A&B from CPT 1
Thank you
WPBeginner Support
Hi Rose,
When creating your taxonomies you can name them differently and allow them to have same labels.
Admin
Gabriela
How to add a second custom post type?
I tried repeating the code only changing the names of my CPTs but it doesn’t work…
Any ideas?
Arun Sathiya
The displaying CPT in category pages code works like a charm, thank you! But, there is something that you should note though. The latest post / CPT post tagged with a category name does not show up on that category page immediately. It shows up only on the second similar action – only when another post / CPT post is added to the same category, the first post appears on that category page. is there a fix to this?
Sonjoe
I am using CPT UI. I created custom post type “jobs” and allowed categories. But this same categories is also on Posts. They using the same categories. It is possible, that my custom post type “jobs” will have own categories and classic posts will have own categories? Thanks
Rizky
Same question. Do we need to run a custom query if we want to create a category archive of that custom post type?
fathur
how do you deal with Recent Posts sidebar widget that also list the menu item?
$post_type = array(‘nav_menu_item’, ‘post’, ‘movies’);
WPBeginner Support
you can remove nav_menu_item
Admin
vivek
Thanks for the great tutorial here.
Can you please tell me how can i show custom posts by category on separate page.
Default category page is working properly, it displays the posts. but when I tried to display posts under the same category in a different page, it returns empty array. Pleas help
Matthew Wright
Thank you! This is awesome. It solved simply a problem that I’ve been working on with much more convoluted solutions (that didn’t work).
One question though, I’ve never added the taxonomy declaration when creating custom taxonomies for my CPTs and the taxonomies have always worked. I added just the ‘category’ to see what would happen and it worked beautifully, added post categories to my CPT with no ill effect on the existing custom taxonomies.
Is it best practice though to put custom tax in the CPT arguments?
Thanks again for sharing
WPBeginner Support
Yes, if you already know that you will be using a custom taxonomy for that particular post type.
Admin
Daisy
Custom code for adding category and sub categorues in custom post type.If i choose $post_type = array(‘nav_menu_item’, ‘post’, ‘myPostType’); code for displaying my categories it only display that category only but if i reuse the same function for another category it won’t work.please give any idea
Chris Najman
Hello. I successfully created a custom post type (Artwork) in functions.php. Then I created a custom taxonomy (‘hierarchical’ => true) which means I can add custom categories when I create a new Artwork post.
I was wondering if, within the same custom taxonomy function, I could pre-define the categories for the Artwork post.
What I want are only two categories to be selectable by the user : 1) Painting, 2) Sculpture. I don’t want the user to be able to add any more categories. Ideally, the user would be ‘forced’ to choose one or the other category, i.e. the post wouldn’t be publishable unless a category had been chosen.
Is this possible?
WPBeginner Support
It is possible by creating custom user roles with limit on creating categories.
Admin
Mau Ferrusca
I am adding more then one categories to several custom post types.
How can you add more than one in the line:
$post_type = array(‘nav_menu_item’, ‘post’, ‘myPostType’);
?
Thanks!
Mau Ferrusca
Already solved my problem.
Perhaps instinctively, I started listing my custom post types as an array, but turns out that all I needed to do was to list them inside the $post_type object, separated with commas.
Thanks!
Eren
Hi, i have a question. I’m using a plugin as a post type and it has its own category area. I want to use my wordpress’s core categories in this plugin but couldnt able to do it so far. I used the plugin above and after that it creates 2 categories area under the plugin and when i want to show the posts under that post type with categories, it shows nothing as the plugin’s own category list is empty. I tried lots of things including manipulating the code of the plugin yet none solved my issue. Is there a way you can suggest?
Davide
Did you find any solution for this issue?
David
Has this article been updated recently to work with the current version of WP? I noticed the plugin used hasn’t been updated in over 2 years, which makes me a little hesitant to use it.
David
Sorry, nevermind, I see the updated info.
Piyush
Thanks for the great tutorial. can you please tell me how can i show custom posts by category on separate page. Lets say i have CPT “talent” and a category “art”. I have two different posts one is created under CPT “talents” and other is general post created under “posts”. Both posts are assigned to “art” category(this is common for both CPT and general posts). Now i know that for the general posts you can view the category page as example.com/category/art/. For CPT category page i guess the url should be example.com/talent/art/ but it is giving me 404 error.
WPBeginner Support
Try updating your permalink structure. Simply visit Settings » Permalinks and then click on the save changes button without changing anything.
Admin
Robert
Well done thanks!
I have a couple of troubles:
– in “Categories” widget appear the Categories of Custom Post Types but on clic it returns a “nothing found” message.
– in “Recent Posts” widget don’t appear Custom Post Type Posts.
There is a way to fix it?
Hemang Rindani
Interesting article. WordPress is a powerful enterprise web content management service that allows a company to have efficient websites that suits their business requirements. It’s easy to use and effortless tools makes the development look easier. From responsive design to security and expand-ability, WordPress CMS has it all in it however, searching the right tools for a website is important.
Adding category to custom post type sometimes becomes a necessity for differentiating one post from other where the methods mentioned in the article will be useful. Using plugin will be an easy method where even a non-technical person will be able to implement this, however with this article, I don’t feel that using code to add this feature by a non-technical person will be difficult.
Thanks for sharing.
Emily Johns
Great tips, I’ve got the way to fix my problem with this. Thanks!