How to Add Categories to a Custom Post Type in WordPress

WordPress gives you the ability to add built-in categories to custom post types. Categories are built-in taxonomy which are used to group contents in WordPress website. But they are only available for posts by default in WordPress. Despite WordPress’s ability to add categories to custom post type, it does not automatically add categories in custom post types. So this article shows how to add Categories to a custom post type in WordPress.

How to Add Categories to a Custom Post Type in WordPress

You can use plugin to create custom post type and add categories to them. Custom Post Type UI WordPress plugin allow you to create custom post type and categories and can be assigned to custom posts easily. After installing Custom Post Type UI plugin, go to visit CPT UI >> Add/Edit Post Types to create a new custom post type or edit already created custom post type. If you need to create custom post type, you can easily create your required custom post type from this plugin or if you have already created custom post type from this plugin, you will see them easily there and what you need to do is just select the custom post type which you need to add categories.

Then, scroll down to the bottom and there you will see the Built-in Taxonomies option. Check the box next to categories (and tags) and save your custom post type.

How to Add Categories to a Custom Post Type in WordPress

In this way, you can easily create your own custom post and can add categories to custom post type. After creating Reviews custom post type, here’s what it looks like while creating content under that custom post.

How to Add Categories to a Custom Post Type in WordPress

But still, you can’t find them in archive pages of existing categories. Because WordPress does not automatically add them to the archive pages for existing categories. That means, if you create a custom post type called “review” and used the native WordPress category” to categorize them, Simply navigating to the archive for that category will not show your custom post type in that archive page.

For example, I have created a review site and used standard WordPress category for that custom post. Let’s say I decided to use “Hosting” category. Navigating to that category archive in my site, namely http://sitename.com/category/hosting will not show the custom post type I just created  – even though I select the Hosting category. Because without adding some code on theme’s functions.php file, site can’t display the custom post types in category archive page.

So, if I would like to show up my custom post types in archive page for my site’s standard categories, I have to add the following code to theme’s functions.php file.

Also Read:   Setup Contact Form 7 Plugin in WordPress

function add_custom_types_to_tax( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {
// Get all your post types
$post_types = get_post_types();
$query->set( ‘post_type’, $post_types );
return $query;
}
}
add_filter( ‘pre_get_posts’, ‘add_custom_types_to_tax’ );

Only adding this code will enable to see all the custom posts in related archive page. In this way, you can easily create custom post type and add categories to them.

Here’s before adding the code in theme’s functions.php

No Posts Found

And after adding that code in theme’s functions.phpCustom Post Under Hosting Category

I think you have understood how to create custom post type and add categories to them by plugin method. But if you want to know code level.

Go to CPT UI >> Register Type/Taxes, then you will see code as shown in fig below. Click on code, then you will get all the required code. Copy all the codes and paste them in your theme’s functions.php file. In this way, those code works exactly exactly the same as that of plugin. And if you disable plugin, you will have custom post there in your site.

how to add categories to a custom post type in wordpress

That’s all, I hope this article definitely helped you to add categories to your custom post type in WordPress. If you liked this article, then please don’t forget the share it on social networking sites such as Facebook, Twitter, Google Plus etc.

The following two tabs change content below.
Kamal Tamang
Kamal Tamang is a freelance web developer and has completed his Bachelor’s degree in Computer Engineering. He is a SEO expert, web article writer and has written 100+ search engine friendly web article for different websites. You can drop mail at kamaltmg2071@gmail.com to contact him.

Share This Post