wordpress custom taxonomy

How To Create A Custom Taxonomy In WordPress?

WordPress lets you use Categories, Tags, Link Category, and Post Format to organize your content as a default feature. Yet, if you want to create a WordPress custom taxonomy, what that implies is that you’re aiming to customize further the way you sort your content.

Do you want to learn how to create a custom taxonomy in WordPress? You definitely should! Creating taxonomies is powerful, and the ways to do so are either with or without a plugin. Before getting on to the methods, let’s first establish an understanding of what taxonomy means.

WordPress Taxonomy Defined

A well-structured content plan makes it easier for visitors to navigate through your website’s content. 

How do you structure your content to increase its effectiveness? The answer is, we use taxonomy – the process of organizing content according to certain properties and criteria. The word ‘taxonomy’ is derived from ‘Linnaean taxonomy’, a biological classification method. It is commonly termed as a method of organizing groups of posts and custom post types in WordPress. 

In WordPress, by default, you can use the four taxonomies that come with the CMS. Yet, Categories & Tags are the two most commonly used among the four. By using these default taxonomies, you can organize your WP blog posts to an extent. But what if you’re using a custom post type? In that case, depending on the type of content, categories, and tags may not always be suitable.

To understand the need for creating custom taxonomies in WordPress, we can take the example of content about movies (custom post type). We can thus illustrate how a custom post can use custom taxonomies for better-structured content.

Let’s say the custom post type is called ‘Movies’. To sort it, we need to be using a custom taxonomy called ‘genres’. You can add 66hgenre terms like Comedy, Thriller, Romance, and any others you want. In essence, you’re sorting movies by genre to make it easier for readers. 

You can further use taxonomies to group movies hierarchically. This means you can have main topics like Fiction, Non-fiction, Children, Short-films, Foreign language, among others. Under each main topic, you can have sub-topics. For instance, Foreign languages would have French, Spanish, German as subtopics. 

Next, we’ll show you how to create custom taxonomies in WordPress. 

How a WordPress Custom Taxonomy is Created?

To create a custom taxonomy in WordPress, you can use either of two methods. The first method is – using a plugin. The easier of the two methods!

The second method is manual (through coding), involving creating a taxonomy functions PHP code to be inserted inside the theme’s functions.php file. This method can be complicated for non-experienced users.

Let’s take a look at each in turn.

1. Creating a Custom Taxonomy in WordPress Using a Plugin

Once again, using a plugin to create a custom taxonomy is undoubtedly easy among the two methods. You don’t need to be a developer. Anyone, including a beginner, can use any of the popular plugins with ease. With a plugin, it couldn’t be easier to keep adding additional taxonomies as you go.

All the popular plugins operate similarly. This article will illustrate how to create a custom taxonomy using the ‘Custom Post Type UI’ (open source/free) plugin and the ‘Pods’ plugin. Though we’ll demonstrate these two plugins, another popular plugin worth checking out is ‘Toolset Types’, which comes in both paid and premium versions.

Each of the popular plugins mentioned above is strongly backed by author support. Author support is essential when it comes to taxonomies.

Now, let’s look at how creating a custom taxonomy is a breeze using either of these two plugins.

# Using the ‘Custom Post Type UI’ (Open Source/Free) Plugin

Firstly, install and activate the ‘Custom Post Type UI’ plugin. To install it, go to your wp-admin dashboard and click on ‘Plugins’ and search for the plugin. Once found, click the ‘Install Now’ button to activate the plugin.

Next, in your WordPress admin area, navigate to the ‘CPTUI’ menu > ‘Add/Edit Post Types’ and create your first taxonomy.

To begin with, you need to provide a “slug” for the taxonomy. A slug is a term used for the user-friendly and URL valid name of a post.

The slug can only contain letters and numbers as used in the URL and the WP search queries. You will note that the letters are automatically converted into lowercase.

Next, enter the ‘Plural Label’ and Singular Label’ names of your custom taxonomy. 

wordpress custom taxonomy using plugin

After filling in the Plural and Singular names, you may optionally click on the link ‘Populate additional labels based on chosen labels’. Doing so allows the plugin to auto-fill the remaining label fields.

To describe your post type, scroll down to the ‘Additional Labels’ section. The additional labels you named are used in your WP dashboard and prove to be quite helpful whenever you’re editing content for the specific custom taxonomies.

How To Create A Custom Taxonomy In WordPress

Next, scroll further down the screen to the ‘settings’ option to set different attributes for every taxonomy you’ve created. For example, you may choose to make a taxonomy hierarchical, wherein ‘Topics’ will have subtopics.

Once you’re done scrolling through all types of optional settings and making desired selections along the way, click on the ‘Add Taxonomy’ button at the very bottom to save the custom taxonomy.

Once the custom taxonomy is saved, you can start using it. To use it, head over to your WordPress content editor and edit the post type associated with that particular taxonomy.

# Using the ‘Pods’ (Open Source/Free) Plugin

Start by installing and activating the ‘Pods’ plugin. To install it, go to your wp-admin dashboard and click on ‘Plugins’ and search for the ‘Pods– Custom Content Types and Fields’ plugin. Once found, click the ‘Install Now’ button and activate the plugin.

wordpress custum taxonomy by pods plugin

Next, to create WordPress custom taxonomies, click on ‘Create New’ for adding the taxonomy (as a new taxonomy) to an existing “Post Type”.

Next, select the content type and set the labels (singular and plural ). As per our earlier example, to create a taxonomy called “Movies”, select “Custom Taxonomy (like Categories or Tags)” from the “Content Type” drop-down menu.

Once done, in the ‘Singular label’ and ‘Plural label’ fields, enter the labels for your new taxonomy. For instance, we’ve entered “Movie” and “Movies”, respectively. After that, by clicking “Next step”, you would have created your new taxonomy. 

Create A Custom Taxonomy In WordPress

Subsequently, you need to make your custom taxonomy work. For that, you have to communicate to ‘Pods’ that you would like to associate the new taxonomy to at least one post type in WordPress so that it shows up in the admin section.

To make it work, click on the “Advanced Options” tab, then scroll down to “Associated Post Types” to select the post types you will be using this taxonomy for. 

Finally, click on ‘Save’! You’ll then see a new menu item under your selected post type – your new custom taxonomy.

The Result

The default post type and default WordPress taxonomies (categories and tags) from our wp-admin dashboard can be seen by clicking on ‘Posts’ on your wp-admin dashboard:

posts on wp admin dashboard

In our “Movies” example, as per our taxonomy selection and creation through the ‘Pods’ plugin, the result is the following:

movies in wordpress admin dashboard

The custom post type is ‘Movies’, having its own default taxonomy ‘Genres’. ‘Action (or Romance, Comedy, etc.) are the child item (term) for the sake of our example. Our taxonomy – ‘Genres’, can have any number of terms. The term created by us, ‘Action’, can have sub-level terms like ‘Action Comedy’ and ‘Action Romance’. 

2. Manually Creating Custom Taxonomies in WordPress (with code)

Using a plugin instead of the manual method has several advantages. For instance, you may go for the plugin if you’re not experienced with coding. Further, the plugin will not crash your site, and it has a user-friendly interface. Both the recommended plugins are free, which is another great reason to try them. Yet, there has to be some incentive to prefer the manual method! 

# Why create a custom taxonomy manually (with custom coding)?

The manual method uses coding, which can be quite challenging for non-experienced users. A particular scenario that favors the manual method over the plugin method is when you’re sure you need to set only a few taxonomies at the start and never after that. In such a case, custom coding aids in avoiding a slowing down of your WordPress hosting caused by adding a plugin.

In this article, we’ll illustrate how to manually create custom taxonomies starting with a hierarchical custom taxonomy (works like ‘Categories’), which can have parent and child terms. After that, we’ll show you how to create a non-hierarchical custom taxonomy (works like ‘Tags’), also optionally having parent and child terms.

If this (custom coding) is what you want to do, we’ve laid out the process below to make it as easy as possible for you:

To Create A Hierarchical Custom Taxonomy:

Before you begin, it’s important to know the following:

  • Know where to place the code – The logical place is in a file that is loaded upon initialization. Ideally, it should be placed at the very bottom of the functions.php file of your theme (or child theme, if being used).
  • To avoid bringing your site down, avoid making syntax errors – It is recommended you create a backup of your website beforehand if you need to restore the backup if something bad happens. 

Now let’s begin coding in the theme’s functions.php file to create a hierarchical custom taxonomy (that works like ‘Categories’):

# hook into the init action and call create_movie_taxonomies when it fires

add_action( ‘init’, ‘create_genres_hierarchical_taxonomy’, 0 );

 

# create a custom taxonomy name it Genres for your posts

function create_genres_hierarchical_taxonomy() {

 

# Add new taxonomy, make it hierarchical like categories

 

# first do the translations part for GUI

  $labels = array(

    ‘name’ => _x( ‘Genres’, ‘taxonomy general name’ ),

    ‘singular_name’ => _x( ‘Genre’, ‘taxonomy singular name’ ),

    ‘search_items’ => __( ‘Search Genres’ ),

    ‘all_items’ => __( ‘All Genres’ ),

    ‘parent_item’ => __( ‘Parent Genre’ ),

    ‘parent_item_colon’ => __( ‘Parent Genre:’ ),

    ‘edit_item’ => __( ‘Edit Genre’ ),

    ‘update_item’ => __( ‘Update Genre’ ),

    ‘add_new_item’ => __( ‘Add New Genre’ ),

    ‘new_item_name’ => __( ‘New Genre Name’ ),

    ‘menu_name’ => __( ‘Genres’ ),

  );

 

# Now register the taxonomy

  register_taxonomy(‘genres’,array(‘movies’), array(

    ‘hierarchical’ => true,

    ‘labels’ => $labels,

    ‘show_ui’ => true,

    ‘show_in_rest’ => true,

    ‘show_admin_column’ => true,

    ‘query_var’ => true,

    ‘rewrite’ => array( ‘slug’ => ‘genre’ ),

  ));

 

}

In the above code, we’ve used the taxonomy name and labels as per our example. Hence, you would have to replace the taxonomy name and labels as per your post type. For example, this particular taxonomy is associated with the “Movies” post type, yours could be “Books”, “Sports”, or any other niche you’re into.

Create A Non-Hierarchical Custom Taxonomy:

For coding in the theme’s functions.php file to create a non-hierarchical custom taxonomy (that works like ‘Tags’), add the following code to add the taxonomy ‘languages’ (as in breaking down movies according to language, in our example) :

# hook into the init action and call create_languages_nonhierarchical_taxonomy when it fires

 

add_action( ‘init’, ‘create_languages_nonhierarchical_taxonomy’, 0 );

 

function create_languages_nonhierarchical_taxonomy() {

 

# Labels part for the GUI

 

  $labels = array(

    ‘name’ => _x( ‘languages’, ‘taxonomy general name’ ),

    ‘singular_name’ => _x( ‘language’, ‘taxonomy singular name’ ),

    ‘search_items’ => __( ‘Search languages’ ),

    ‘popular_items’ => __( ‘Popular languages’ ),

    ‘all_items’ => __( ‘All languages’ ),

    ‘parent_item’ => null,

    ‘parent_item_colon’ => null,

    ‘edit_item’ => __( ‘Edit language’ ),

    ‘update_item’ => __( ‘Update language’ ),

    ‘add_new_item’ => __( ‘Add New language’ ),

    ‘new_item_name’ => __( ‘New language Name’ ),

    ‘separate_items_with_commas’ => __( ‘Separate languages with commas’ ),

    ‘add_or_remove_items’ => __( ‘Add or remove languages’ ),

    ‘choose_from_most_used’ => __( ‘Choose from the most used languages’ ),

    ‘menu_name’ => __( ‘languages’ ),

  );

 

# Now register the non-hierarchical taxonomy like tag

 

  register_taxonomy(‘languages’,’movies’,array(

    ‘hierarchical’ => false,

    ‘labels’ => $labels,

    ‘show_ui’ => true,

    ‘show_in_rest’ => true,

    ‘show_admin_column’ => true,

    ‘update_count_callback’ => ‘_update_post_term_count’,

    ‘query_var’ => true,

    ‘rewrite’ => array( ‘slug’ => ‘language’ ),

  ));

}

What’s the difference between the two sets of codes?

You’ll notice that the hierarchical argument value is true for the category-like taxonomy and false for tags-like taxonomies. Moreover, in the labels array for a non-hierarchical tags-like taxonomy, we have added null for both parent_item and the parent_item_colon arguments. This is to ensure nothing is shown in the ‘User Interface’ to create parent items.

How To Display The Custom Taxonomies You’ve Created?

Once you’ve entered the coding to create custom taxonomies (and perhaps a few terms), you need to take measures to ensure your WordPress theme displays them. The measure involves adding some code to the WP theme (or to the child theme) template files (where you want to display the terms). This could be the single.php, content.php, or other files located inside your WP theme’s templates-parts folder.

To display posts, the default location of your custom taxonomies is the archive.php template. Alternatively, you may create a custom archive display for your posts by creating taxonomy-{taxonomy-slug}.php.

Once you’ve selected the template part where you want the terms displayed, the code to be added is:

<?php the_terms( $post->ID, ‘languages’, ‘languages: ‘, ‘, ‘, ‘ ‘ ); ?>

Finally…

Did this article serve you well in understanding how to create a custom taxonomy in WordPress using a plugin and manual coding? Use the comments section to share your experience and any additional tips you may have. 

Also, check out our guides titled, “What To Do When WordPress Keeps Logging You Out?“, and “How to Fix White Screen of Death? When WP-Admin Gives Blank page“, among many others.

Leave a Comment

Your email address will not be published. Required fields are marked *