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
If you created your custom post type by adding the code in your themeβs functions.php file or a site-specific 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.
1 | '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:
1 | '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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 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 or a site-specific plugin.
1 2 3 4 5 6 7 8 9 10 11 12 | 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 ; } } |
Donβt forget to replace βmoviesβ with the name of your own custom post type. You can now visit a category archive page and it will display your entries from your custom post type.
We hope this article helped you learn how to add categories to your custom post type in WordPress. You can use the same methods to add tags to your custom post types as well. See our guide: categories vs. tags to learn more.
You may also want to see our expert pick of the must have WordPress plugins to grow your website.
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.