Add Default WordPress Categories and Tags to Custom Post Types

Add Default WordPress Categories and Tags to

You can add default WordPress Categories and Tags taxonomies to Custom Post Types through passing taxonomies parameters to arguments. In previous article we had learnt how to create CPT using code in functions.php. Here I will explain how can you use WordPress default built-in taxonomies like Categories and Tags with those CPT.

We had created Book as Custom Post Type in previous tutorial. We are adding Default WordPress Categories and Tags support to our CPT in this step. For a better understanding, surely read all articles in this series.

Add Default WordPress Categories and Tags to Custom Post Types

Before we continue, let’s look what are included in before and after this article in CPT series:

Create custom post types

WordPress Categories and Tags Taxonomies to CPT 

Custom taxonomies for custom post type content

Recalling from previous article, check my_posttype_and_taxonomy() function and uncomment line #28. The taxonomies parameter in arguments passed, allows us to enable WP Category / Tag to support our CPT. Where as category and post_tag values are to add Categories and Tags to our manually created custom post type respectively.

So our $args variable after removing comment symbol from line #28 would be:

We have tweaked our function a little to add WP Categories & Tags when we’re manually creating Custom Post Type using code. You can remove post_tag from array if you don’t need Tag taxonomy.

Support Default WordPress Categories and Tags in CPT created by a Plugin

Instead of manually creating CPT, if you’re using any plugin to do this, you can also support these two WP default taxonomies to that CPT object created by Plugin.

Don’t forget to replace post_type with actual CPT slug. you can get this slug by post_type parameter shown in url after you click that CPT menu. If you have already registered custom taxonomy, you can also pass it as first argument in register_taxonomy_for_object_type() function. Then the post_type will support that custom taxonomy too. Just, you might need to specify init action hook call priority here. 

Display all Post Types on Category Pages

Now the last problem is by default WordPress doesn’t show posts from our CPT on category pages even default WordPress Category taxonomy is used in our CPT. In other words, whenever you would visit index or category page, you will see only posts who belong to default Post post type.

Add the following code in theme’s function.php or your plugin specific file (recommended) to display our Custom Post Type, on same Category Archive pages used by Post post type.

Note: Replace book with your post_type and nav_menu_item is needed for menu to work.

If your CPT doesn’t need WP Categories / Tags, you don’t need these code. Also if you’re using plugin I mentioned previously to create Custom Post Types, they already provide you interface to enable default taxonomy. 

So here our CPT is also using Default WordPress Categories and Tags taxonomies. Continue to next article where we are adding custom taxonomies rather the defaults. If you like the series, don’t forget to spread knowledge by sharing article. Your comments, questions and feedback are most welcome.

You Might Interested In