How To Create Custom Taxonomy In WordPress

  • Post author:
  • Post category:WPDEV
  • Post comments:0 Comments
  • Post last modified:April 13, 2021

By default, WordPress provides you all the things to manage and run your website. You can create posts and group posts in certain categories. But if you need some custom features then custom post types and custom taxonomies will help you. In this tutorial, you’ll learn how to create custom taxonomies for custom post type and built-in post types.

As well as you will also learn how to create an archive page for custom taxonomies and display posts from custom taxonomy terms in your WordPress website.

What is Taxonomy in WordPress

In WordPress, taxonomy refers to a way of categorisation or classification of posts and custom post types together. Taxonomy is an easy method to display related content on your website. By default WordPress offers two basic taxonomy types: Categories and Tags. 

You can also create a custom taxonomy for custom post type. For example, you have a custom post type called Movies. You can create a custom taxonomy called Movie Types. For  the ‘Movie’ types you can add multiple terms like: Action, Romantic, Comedy, Drama, Fantasy. This will allow you to sort your movies by each movie type. So we can relate a taxonomy as parent and terms as childrens.

You can display taxonomy content in a theme using taxonomy templates. With this template, you can use built-in taxonomy functions to display your custom data.

For additional information about taxonomies and terms, you can watch this video.

I am sure the above video and explanation have helped you to know what taxonomy is. So let’s learn how to create custom taxonomy. To create custom taxonomy, we will use two methods: Easy and Manual. First method is easy, here we will install a taxonomy plugin to help website owners who don’t have coding knowledge. Second method is manual, here we will create custom taxonomies without plugin by writing code.

If you don’t want to make any change on your theme’s files then choose the easy method.  

After creating custom taxonomies in theme, you’ll also learn how to display data of custom taxonomies using a taxonomy template. 

Create Custom Taxonomies using Taxonomy Plugin

Let’s start creating a custom taxonomy installing a taxonomy plugin. Here we will be registering custom taxonomies for the post type.

In this tutorial, I am using a Custom Post Type UI plugin developed by WebDevStudios. So first install this plugin.

Here I will show you how to register custom taxonomy for built-in posts type and custom post type. 

1. Add a Custom Taxonomy for built-in Posts Type

Let’s start creating custom taxonomies for built-in post type. Go to CPT UI option >> Add/Edit Taxonomies to add a new taxonomy.

Look at the screenshot above, here I have created a taxonomy called Movies and attached it to Posts type. 

First you should  give a taxonomy name/slug which is used for various queries for taxonomy content.  Taxonomy slug should only contain alphanumeric, latin characters. You should use underscores in place of spaces if your taxonomy slug contains two or more words.

Second, give a plural label for your new taxonomy. Plural label is used for the taxonomy admin menu item. 

Third, give a singular label for your new taxonomy. Singular label is used when a singular label is needed. 

At last, you should choose a post type. We are creating a custom taxonomy for default post type so we need to choose the first option (i.e., Posts).

Once a new taxonomy is created, it will appear under Posts. There you can add taxonomy Terms like categories.

You can also find a taxonomy field in the post edit area. Like categories, you can categorize your posts under a taxonomy term.

 2. Add a Custom Taxonomy for Custom Post Type

First, we will create a custom post type using CPT UI plugin. Go to CPT UI option >> Add/Edit Post Types.

Look at the screenshot above, here I have created a custom post type called Products.

First, you should give a post type name/slug. Like taxonomy slug names, it should only contain alphanumeric, latin characters and underscores in place of spaces.

Second, give a plural label for your new custom post type. Plural label is used for the post type admin menu item.

Last, give a singular label for your new custom post type. 

Once you create a custom post type, it will appear in the WordPress dashboard.

Our next step is to attach custom taxonomy for custom post type. Again Go to CPT UI option >> Add/Edit Taxonomies

Look at the screenshot above, here I have attached my new custom taxonomy to Products post type. 

Create Custom Taxonomies without Plugin

In this method, we will create a custom taxonomy using register_taxonomy() function. Here also I will show you how to create a custom taxonomy for built-in Posts type and custom post type.

If you have basic knowledge of WordPress theme files, you can follow the steps explained below.

1. Add a Custom Taxonomy for built-in Posts Type

Add the following code in your theme’s functions.php file to create a hierarchical custom taxonomy for built-in Posts type. 

add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
    register_taxonomy(
        'custom_taxonomy_name',
        'post',

        array(
            'labels' => array(
                'name' => 'Product Category',
                'add_new_item' => 'Add New Product Category',
                'new_item_name' => "New Product Type Category"
            ),
            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => true
        )
    );
}

In the above code, you must replace custom_taxonomy_name by your own taxonomy name like: Brand, Topics.  

2. Add a Custom Taxonomy for Custom Post Type

Let’s start creating a custom post type first. Add the following code in your theme’s functions.php file to add a new custom post type.

Or you can create a mu-plugins folder in your theme’s plugin directory. Then create a file called “custom_post_type.php”. Custom post type will exist even if you change the current theme.

<?php 
//Custom Post Type Function
function product_post_types()
{
	register_post_type('product', array(
		//'rewrite' => array('slug' => 'products'),
		'supports' =>array('title', 'editor', 'thumbnail', 'comments'),
		//'has_archive' => true,
        'public' => true,
        'menu_icon' => 'dashicons-cart',
        'labels' => array(
           'name' => 'Products',
           'add_new_item'=> 'Add New Custom Post',
           'edit_item' => 'Edit Post',
           'all_items' => 'All Posts',
           'singular_name' => 'Custom Post'
        )
	));
}
add_action('init', 'product_post_types');
?>

In the above code, you must replace product by your own post type name like: Movies, Books.

I am using a cart icon for my product post type. If you want to change it, then search a matching dash icon for your post type in WordPress dash icons repository.

Next, we will register a custom taxonomy for custom post type. Add the following code in your theme’s functions.php file or in custom_post_type.php file that is located in the mu-plugins folder.  

add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
    register_taxonomy(
        'custom_taxonomy_name',
        'product',

        array(
            'labels' => array(
                'name' => 'Product Category',
                'add_new_item' => 'Add New Product Category',
                'new_item_name' => "New Product Type Category"
            ),
            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => true
        )
    );
}

In the above code, you must replace custom_taxonomy_name by your own taxonomy name  and product by your own post type name.

Display Custom Taxonomy Content

We will create a taxonomy template to display content of custom taxonomy like taxonomy term name, title and content of custom post type.

– taxonomy-{taxonomy}.php – Create a taxonomy template for one specific taxonomy name. For example; if the taxonomy name is Topics then we could create a taxonomy-topics.php file.

– taxonomy.php – Used for all custom taxonomies. 

– taxonomy-{taxonomy}-{slug}.php – Create a template for a taxonomy name and a specific term. For example; if the taxonomy name is Topics and the term is Romance then we could create a taxonomy-topics-romance.php file to display all content from the Romance term.

To display terms that you added to a custom taxonomy, add the single line code below in your single.php, archive.php, index.php and anywhere you want to display.

<?php the_terms( $post->ID, 'topics', 'Topics: ', ', ', ' ' ); ?>

Conclusion

Custom taxonomies can be used in many ways. You can also attach custom taxonomies to pages, custom meta boxes, custom post types, and built-in posts type. With custom taxonomies, you can make WordPress CMS more advanced and highly customizable. 

If you have any problems then feel free to tell me about your issues in the comment. 

Leave a Reply