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.
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,
/*
* 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 );
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,
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.