,

How to Create Custom Taxonomies in WordPress

A WordPress taxonomy is a way to organize groups of posts and custom post types. This gives you the flexibility to group posts, pages, etc. for your site needs. Fortunately, WordPress comes with some inbuilt custom taxonomies that make it possible to organize contents and posts but unfortunately, those inbuilt taxonomies might not meet your specific need.

This is where custom taxonomy comes in and in this article, I am going to walk you through how to create custom taxonomies to meet your specific site needs.

Custom Taxonomies

Custom Taxonomies are useful when you want to create custom groupings/classifications that best explain the relationships between your posts. WordPress provides us with the tools to create Custom Taxonomies and we’re going to look at them in this article.

Why Use Custom Taxonomies?

Custom taxonomies are very important especially when you need your site tailored to your specific needs compared to the traditional “Categories” and “tags” that WordPress comes with. For example, you might want to build a blog website that features articles about “Movies”. One way to organize the blog might be to create a Custom Taxonomy called “Fiction” to store all the blog posts on fiction, then a Taxonomy called “Adventure” to store posts on Adventure and probably another Taxonomy called “Action” to store posts on movies with an action genre.

The key advantage of using Custom Taxonomies is:

  • You can reference them, for example, “Fiction” and “Adventure” independently of Categories and Tags.
  • They get their own menus on the Admin Dashboard.
  • It enables you to create custom User Interfaces (UIs) for users on the Admin dashboard which allows users to intuitively group their posts correctly

Creating Custom Taxonomies in WordPress

There are two ways to create custom taxonomies in WordPress:

  1. The Plugin Method
  2. A programmatic way by using one of WordPress’ inbuilt functions.

Creating Custom Taxonomies With A Plugin

In order to create a custom taxonomy using a plugin, the first thing you need to do is install and activate the Custom Post Type UI plugin.

First, locate the plugins directory in the dashboard.

Second, use the search bar to search for CPT UI plugin and install it.

After installation, navigate to the CPT UI menu on the left side of the dashboard. Click on “Add/Edit Taxonomies” to create your first Taxonomy. You can fill the form with the settings in the example below. In this example, I used “Movies” as a Custom Post Type.

If you created a custom post type and wanted to attach the new taxonomies to it, it will show up under the Attach to Post Type settings.

You can also choose whether you want your taxonomies to be “hierarchical or non-hierarchical” as seen below.

If your taxonomy choice is hierarchical, this means your taxonomy titled “Movie” can have sub-topics. For example, a Movie with a genre of Fiction can have sub-genres like Fantasy, Thriller, Mystery, and more. There are many other settings further down your screen in your WordPress dashboard, but you can leave them as-is for this tutorial.

Next, you can click on the “Add Taxonomy” button at the bottom to save your custom taxonomy.

Great! You’ve created your first custom taxonomy.

To check the taxonomy you just created, you can navigate to the WordPress sidebar, click on “Posts” or “Pages” or any other custom post type you selected when creating the taxonomy, and your new taxonomy should be available to you.

For example, after creating my “Movies” taxonomy, I could view it under both “posts” and “pages” because I selected the taxonomy to be available to both on creation.

Now that you’ve created a custom taxonomy, you can go to either “pages or posts”, create a new post, and attach the “Movies” taxonomy to the post to help categorize it.

Creating Custom Taxonomies Programmatically

We can create both hierarchical and non-hierarchical programmatically using PHP. Note that the Hierarchical type of taxonomy is category-like while the Non-Hierarchical type of taxonomy is tag-like.

Creating a Non-hierarchal Taxonomy with code

$plural_name = 'Taxonomy Plural name';
$singular_name = 'Taxonomy Singular name';
$decription 'Descrription of the Taxonomy';
$cpt_name = 'Custom Post type or In bult Post Type';
$taxonomy_name = 'Taxonomy Name';

//hook into the init action and call create_movies_nonhierarchical_taxonomy when it fires
add_action( 'init', 'create_movies_nonhierarchical_taxonomy', 0 );
 
function create_movies_nonhierarchical_taxonomy() {
    register_taxonomy(
        $taxonomy_name,
            array($cpt_name),
            array(
                'labels' => array(
                    'name' => $plural_name,
                    'singular_name' => $singular_name,
                    'search_items' => 'Search ' . $singular_name,
                    'all_items' => 'All ' . $plural_name,
                    'parent_item' => null
                    'parent_item_colon' => null
                    'edit_item' => 'Edit ' . $singular_name,
                    'view item' => 'View ' . $singular_name,
                    'update_item' => 'Update ' . $singular_name,
                    'add_new_item' => 'Add New ' . $singular_name,
                    'new_item_name' => 'New ' . $singular_name . ' Name',
                ),
                'description' => $description,
                'public' => true,
                'hierarchical' => false,
                'show_ui' => true,
                'show_in_menu' => true,
                'show_in_nav_menus' => false,
                'rewrite' => false,
                'query_var' => true,
            )
      );
}

Creating a Hierarchal Taxonomy with code

$plural_name = 'Taxonomy Plural name';
$singular_name = 'Taxonomy Singular name';
$decription 'Descrription of the Taxonomy';
$cpt_name = 'Custom Post type or In bult Post Type';
$taxonomy_name = 'Taxonomy Name';

//hook into the init action and call create_movies_hierarchical_taxonomy when it fires
add_action( 'init', 'create_movies_hierarchical_taxonomy', 0 );
 
function create_movies_hierarchical_taxonomy() {
	  register_taxonomy(
		    $taxonomy_name,
		    array($cpt_name),
		    array(
			      'labels' => array(
				        'name' => $plural_name,
				        'singular_name' => $singular_name,
			 	        'search_items' => 'Search ' . $singular_name,
				        'all_items' => 'All ' . $plural_name,
				        'parent_item' => 'Parent'. $singular_name,
				        'parent_item_colon' => 'Parent'. $singular_name . ':',
				        'edit_item' => 'Edit ' . $singular_name,
				        'view item' => 'View ' . $singular_name,
				        'update_item' => 'Update ' . $singular_name,
				        'add_new_item' => 'Add New ' . $singular_name,
				        'new_item_name' => 'New ' . $singular_name . ' Name',
			      ),
			      'description' => $description,
			      'public' => true,
			      'hierarchical' => true,
			      'show_ui' => true,
			      'show_in_menu' => true,
			      'show_in_nav_menus' => false,
			      'rewrite' => false,
			      'query_var' => true,
		    )
	  );
}

Notice the difference between both blocks of code. On line 30, the value for the hierarchical argument is true for category-like taxonomy and false for tags-like taxonomies.

Also, on lines 20 and 21, note that in the labels array for non-hierarchical tags-like taxonomy, I added null for parent_item and parent_item_colon arguments which means that nothing will be shown in the UI to create parent item.

I hope this article has helped you learn how to create and register custom taxonomies in WordPress. If you have any questions, you can put them in the comments below