Recently one of our user asked us if it was possible to add category to a custom post-type they have created. Categories are one of the built-in taxonomy in WordPress. By default they appear only for posts. However, in some scenarios you may want to share them with a custom post type as well. In this article, we will show you how to add categories to a custom post type in WordPress. We will also show you how to display multiple post types on your category archive page.
The Plugin Method
For our beginner level users, we recommend using Custom Post Type UI plugin to creat custom post type. When using Custom Post Type UI plugin, you have the option to associate your custom post type to 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 plug in.
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 on the Advanced Options to the bottom and there you will see the Built in Taxnomies option. 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.
Manually Adding Categories to a Custom Post Type
If you created your custom post type by adding the code in your theme’s functiontype.php file or a site-specific plugins, 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 CPT.
1
'taxonomies'
=>
array
(
'category'
),
It is likely that you may already have this line in the existing code for your CPT with some other custom taxonomy in it. If you do, then you just need to add a comma after that and add category, like this:
1
'taxonomies'
=>
array
(
'topics'
,
'category'
),
Here is a full example code where we have created a custom post type called movies with support for built-in categories.
01
function
custom_post_type() {
02
03
// Set UI labels for Custom Post Type
04
$labels
=
array
(
05
'name'
=> _x(
'Movies'
,
'Post Type General Name'
,
'twentythirteen'
),
06
'singular_name'
=> _x(
'Movie'
,
'Post Type Singular Name'
,
'twentythirteen'
),
07
'menu_name'
=> __(
'Movies'
,
'twentythirteen'
),
08
'parent_item_colon'
=> __(
'Parent Movie'
,
'twentythirteen'
),
09
'all_items'
=> __(
'All Movies'
,
'twentythirteen'
),
10
'view_item'
=> __(
'View Movie'
,
'twentythirteen'
),
11
'add_new_item'
=> __(
'Add New Movie'
,
'twentythirteen'
),
12
'add_new'
=> __(
'Add New'
,
'twentythirteen'
),
13
'edit_item'
=> __(
'Edit Movie'
,
'twentythirteen'
),
14
'update_item'
=> __(
'Update Movie'
,
'twentythirteen'
),
15
'search_items'
=> __(
'Search Movie'
,
'twentythirteen'
),
16
'not_found'
=> __(
'Not Found'
,
'twentythirteen'
),
17
'not_found_in_trash'
=> __(
'Not found in Trash'
,
'twentythirteen'
),
18
);
19
20
// Set other options for Custom Post Type
21
22
$args
=
array
(
23
'label'
=> __(
'movies'
,
'twentythirteen'
),
24
'description'
=> __(
'Movie news and reviews'
,
'twentythirteen'
),
25
'labels'
=>
$labels
,
26
'supports'
=>
array
(
'title'
,
'editor'
,
'excerpt'
,
'author'
,
'thumbnail'
,
'comments'
,
'revisions'
,
'custom-fields'
, ),
27
'hierarchical'
=> false,
28
'public'
=> true,
29
'show_ui'
=> true,
30
'show_in_menu'
=> true,
31
'show_in_nav_menus'
=> true,
32
'show_in_admin_bar'
=> true,
33
'menu_position'
=> 5,
34
'can_export'
=> true,
35
'has_archive'
=> true,
36
'exclude_from_search'
=> false,
37
'publicly_queryable'
=> true,
38
'capability_type'
=>
'page'
,
39
40
// This is where we add taxonomies to our CPT
41
'taxonomies'
=>
array
(
'category'
),
42
);
43
44
// Registering your Custom Post Type
45
register_post_type(
'movies'
,
$args
);
46
47
}
48
49
/* Hook into the 'init' action so that the function
50
* Containing our post type registration is not
51
* unnecessarily executed.
52
*/
53
54
add_action(
'init'
,
'custom_post_type'
, 0 );
Displaying Multiple Post Types on Category Page
By default the catagory 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.
01
add_filter(
'pre_get_posts'
,
'query_post_type'
);
02
function
query_post_type(
$query
) {
03
if
( is_category() ) {
04
$post_type
= get_query_var(
'post_type'
);
05
if
(
$post_type
)
06
$post_type
=
$post_type
;
07
else
08
$post_type
=
array
(
'nav_menu_item'
,
'post'
,
'movies'
);
// don't forget nav_menu_item to allow menus to work!
09
$query
->set(
'post_type'
,
$post_type
);
10
return
$query
;
11
}
12
}
Don’t forget to replace movies with the name of your own custom post type.
That’s all, we hope this article helped you add categories to your custom post type in WordPress. You can use the same methods to add tags to your custom post types as wel