Proper Way to Create Custom Taxonomies in WordPress: A Step-by-Step Guide

How to Create custom Taxonomies in WordPress

WordPress, by default, provides categories and tags for structuring your content. However, you can create custom taxonomies in WordPress to organize your content based on your website requirements.

WordPress is a versatile and powerful content management system (CMS) that allows you to create and manage a wide range of websites, from simple blogs to complex sites. As your website becomes more complicated, you’ll find that organizing your content becomes increasingly important. This is where custom post types and custom taxonomies become handy.

In this article, we will learn to create custom taxonomies in WordPress.

What are Taxonomies in WordPress?

Before getting started on how to create custom taxonomies in WordPress, it’s important to understand what WordPress taxonomies are and how they function in WordPress.

Taxonomy in WordPress refers to a method of categorizing and arranging content. It is a fundamental concept used for grouping and categorizing content, primarily for posts and custom post types. Taxonomies are of two types, hierarchical and non-hierarchical. Hierarchical taxonomies allow you to create parent-child relationships between terms whereas non-hierarchical taxonomies don’t have any hierarchical structure.

How to create Custom Taxonomies in WordPress?

You can create custom taxonomies in WordPress using different plugins. If you prefer this approach then you can install and activate plugins like Custom Post Type UI or Toolset Types. These plugins help you create custom taxonomies without writing any code. However, if you want more control and flexibility, you can create custom taxonomies in WordPress manually.

Steps to create custom Taxonomies in WordPress manually

WordPress has a built-in function register_taxonomy to create a custom taxonomy. The function accepts 3 parameters. The first is the taxonomy key, the second parameter is for the post type with which the taxonomy should be associated, and the third one is the array or query string of arguments for registering a taxonomy. In this article, we will create a custom taxonomy ‘genre‘ for the custom post type ‘music‘. You can find information about custom post types here.

Add the following code in the functions.php file of the active theme from the theme directory. Please make sure you work on the child theme if you are using any prebuilt themes.

/*
*
* Creating a function to create our custom taxonomy
*/
function create_custom_taxonomy() {
	$labels = array(
		'name'              => __( 'Genres', 'Taxonomy General Name', 'your-text-domain' ),
		'singular_name'     => __( 'Genre', 'taxonomy singular name', 'your-text-domain' ),
		'search_items'      => __( 'Search Genres', 'your-text-domain' ),
		'all_items'         => __( 'All Genres', 'your-text-domain' ),
		'parent_item'       => __( 'Parent Genre', 'your-text-domain' ),
		'parent_item_colon' => __( 'Parent Genre:', 'your-text-domain' ),
		'edit_item'         => __( 'Edit Genre', 'your-text-domain' ),
		'update_item'       => __( 'Update Genre', 'your-text-domain' ),
		'add_new_item'      => __( 'Add New Genre', 'your-text-domain' ),
		'new_item_name'     => __( 'New Genre Name', 'your-text-domain' ),
		'menu_name'         => __( 'Genres', 'your-text-domain' ),
	);

	$args = array(
		'hierarchical'      => true,
		'labels'            => $labels,
		'show_ui'           => true,
		'show_admin_column' => true,
		'query_var'         => true,
		'rewrite'           => array( 'slug' => 'genre' ),
	);

	register_taxonomy( 'genre', 'music', $args );
}

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

In the above code, the variable $labels defines all the labels for the custom taxonomy. Also, genre behaves as a hierarchical taxonomy. If you want to make it non-hierarchical, you can simply set the ‘hierarchical’ argument to false.

Once you add the code, please go to Settings > Permalinks and click on the Save Changes button which will make the necessary .htaccess changes.

Create custom taxonomies in WordPress
create custom taxonomies in wordpress screenshot2

You should be able to see the Genres taxonomy added to Music Post Type where you can add the respective terms according to your requirements.

create custom taxonomies in wordpress screenshot3

Also, you should be able to select the respective genre while creating a new music post.

And that’s it. This is how you can create custom taxonomies in WordPress. We hope that this article has given you the information you need to create custom taxonomies in WordPress without using any plugins.

FAQs on How to Create Custom Taxonomies in WordPress

How do I create a custom taxonomy in WooCommerce?

WooCommerce uses its own “product” custom post type for product items. So, when registering your custom taxonomy (let’s call it “brand”), follow this article and while registering the taxonomy, simply set the post type parameter to “product” like this: register_taxonomy( 'brand', 'product', $args ). It’s that simple! Now, you have added custom taxonomy in WooCommerce.

What is the difference between taxonomy and category in WordPress?

In WordPress, a “taxonomy” is a larger organizational system that contains “categories” as one type of taxonomy. Categories are often used in a hierarchical form to organize and classify material such as blog entries, containing parent and child categories. In contrast, “taxonomy” refers to the complete system, which includes not just categories but also tags (non-hierarchical) and other custom taxonomies.

How do I create multiple taxonomies in WordPress?

You can create taxonomies in WordPress using the register_taxonomy function, and you can use it as many times as needed. Simply adjust the function’s parameters like the taxonomy key, post type object/array, and $args variable to suit your specific needs. To learn how to create custom taxonomies in WordPress, refer to this article, and repeat the process as necessary to create multiple taxonomies for your website.

Leave a Reply