Add Default WordPress Categories and Tags to Custom Post Types

You can add default WordPress Categories and Tags taxonomies to Custom Post Types through passing taxonomies parameters to arguments. In the previous article, we had learnt how to create CPT using the 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 the 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

Add Default WordPress Categories and Tags to Custom Post Types

Before we continue, let’s look what is 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 the menu to work.

If your CPT doesn’t need WP Categories / Tags, you don’t need these code. Also if you’re using a 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 the article. Your comments, questions, and feedback are most welcome.

Read next: Custom Taxonomies to CPT.

You Might Interested In

2 COMMENTS

  1. Lauren Rodriguez says:

    I have read your blog on Create Custom Post Types and Taxonomies in WordPress.It was very interesting and helpful but I can add some extra points in your article. Here some extra points:
    1.Create a Child Theme. First of all, create a child of your active theme.
    2.Write code in functions.php. Write following code in your child theme’s functions.
    3.Replace the custom post type with your post type name.
    These are some extra points to add to your article. Readers if you are confused about your web and App development , you can get a free consultation at Alakmalak technologies.Visit our site for more information.

    Reply
  2. 罗昂 says:

    Hi!

    Thanks to your post, I’ve already created the tags and categories for my CPT “lesson” (I’n using LifterLMS to manage courses) but the search by tags or categories doesn’t work. I found this snippet which works for CPT categories and tags:

    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’, ‘lesson’); // don’t forget nav_menu_item to allow menus to work!
    $query->set(‘post_type’,$post_type);
    return $query;
    }
    if( is_tag() ) {
    $post_type = get_query_var(‘post_type’);
    if($post_type)
    $post_type = $post_type;
    else
    $post_type = array(‘nav_menu_item’, ‘post’, ‘lesson’); // don’t forget nav_menu_item to allow menus to work!
    $query->set(‘post_type’,$post_type);
    return $query;
    }
    }

    Thanks

    Reply

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.