PHPIntermediate

How to Create Custom Taxonomy in WordPress

PB Pb28 Master Team July 12th, 2022 Intermediate

📦 Get the complete source code for this tutorial

Taxonomy is a feature to group content like posts, and links in a structured way. For example, category and tag are the inbuilt taxonomies in WordPress to group posts. The category is a hierarchical taxonomy and the tag is an example of non-hierarchical taxonomy.

WordPress allows us to create custom taxonomies. It can be either hierarchical or non-hierarchical. It is introduced in version 2.3. Here we are going to see how to create a custom taxonomy in WordPress.

Register Custom Taxonomy

There is a function named register_taxonomy() to add and register our own custom taxonomy in WordPress. We have to hook this function to the init action in functions.php

Here is an example code for creating a custom hierarchical taxonomy named Awards. The value we are giving in the slug option will be used in the taxonomy page URL. And the code is,

php
/*

 * creating the custom taxonomy named 'Awards'

 */

function create_custom_taxonomy() {

	register_taxonomy('awards', 'post', array(

		'labels' => ( 'Awards' ),

		'rewrite' => array(

			'slug' => 'awards', 

			'hierarchical' => true 

		)

	));

}

add_action( 'init', 'create_custom_taxonomy', 0 );

Taxonomy Terms

We can add more groups under taxonomy. These are called taxonomy terms. In this example, we are registering the custom taxonomy for a post. So we can see this under post menu in WordPress Admin. The screenshot is,

custom-taxonomy-menu

By using this menu, we can navigate to the page to add new terms to this custom taxonomy. This screenshot shows the page to manage custom taxonomy terms.

manage_custom_taxonomy

📦 Download the full project files and try it locally