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 Custom Post Types
Before we continue, let’s look what is included in before and after this article in CPT series:
WordPress Categories and Tags Taxonomies to CPT
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:
1 2 3 4 5 6 7 8 9 10 | $args = array( 'labels' => $labels, 'public' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'book'), 'description' => 'Book post type', 'menu_position' => 4, 'menu_icon' => 'dashicons-media-text', 'taxonomies' => array( 'category', 'post_tag' ) ); |
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.
1 2 3 4 5 6 7 8 9 | function add_wp_taxonomy_to_cpt() { //Replace post_type with actual CPT slug register_taxonomy_for_object_type( 'category', 'post_type' ); register_taxonomy_for_object_type( 'post_tag', 'post_type' ); } add_action( 'init', 'add_wp_taxonomy_to_cpt' ); // Get CPT slug from url, here is event // http://localhost/site/wp-admin/edit.php?post_type=event |
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.
1 2 3 4 5 6 7 8 9 | function my_pre_get_posts($q) { if( is_category() ) { $pt = get_query_var('post_type'); if(!$pt) $pt = array('nav_menu_item', 'post', 'book'); // don't forget nav_menu_item to allow menus to work! $q->set('post_type', $pt); } } add_filter('pre_get_posts', 'my_pre_get_posts'); |
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.
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.
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