Manually Creating Custom Taxonomies in WordPress

In WordPress, taxonomy refers to the method of using keywords to organize contents. There are two default taxonomies in WordPress: Categories and Tags. Categories taxonomy is hierarchical, which means you can have main topics and sub-topics. Tags taxonomy is non-hierarchical – you can only have topics and you cannot have sub-topics. This tutorial will teach you how to manually create custom hierarchical and non-hierarchical taxonomies in WordPress.

An Important Reminder: Always make a backup of your site or taxonomies before making any changes to your website structure.

Creating a Hierarchical Taxonomy

Copy the lines of code below and paste them into the functions.php file of your WordPress site. This example is for a car models taxonomy, so remember to edit it to suit your needs.

// Create a hierarchical taxonomy like categories
add_action( 'init', 'create_car_model_taxonomy', 0 );
function create_car_model_taxonomy() {
	$labels = array(
		'name'              => _x( 'Car Models', 'taxonomy general name', 'textdomain' ),
		'singular_name'     => _x( 'Car Model', 'taxonomy singular name', 'textdomain' ),
		'search_items'      => __( 'Search Car Models', 'textdomain' ),
		'all_items'         => __( 'All Car Models', 'textdomain' ),
		'parent_item'       => __( 'Parent Car Model', 'textdomain' ),
		'parent_item_colon' => __( 'Parent Car Model:', 'textdomain' ),
		'edit_item'         => __( 'Edit Car Model', 'textdomain' ),
		'update_item'       => __( 'Update Car Model', 'textdomain' ),
		'add_new_item'      => __( 'Add New Car Model', 'textdomain' ),
		'new_item_name'     => __( 'New Car Model Name', 'textdomain' ),
		'menu_name'         => __( 'Car Models', 'textdomain' ),
	);
	$args = array(
		'hierarchical'      => true,
		'labels'            => $labels,
		'show_ui'           => true,
		'show_admin_column' => true,
		'query_var'         => true,
		'rewrite'           => array( 'slug' => 'car-models', 'with_front' => false ),
	);
	register_taxonomy( 'car-models', ['post'], $args);
}

Creating a Non-Hierarchical Taxonomy

Copy the lines of code below and paste them into the functions.php file of your WordPress site. This example is for an authors taxonomy, so do not forget to edit it.

// Create non-hierarchical taxonomy like tags
add_action( 'init', 'create_author_taxonomy', 0 );
function create_author_taxonomy() {
	$labels = array(
		'name'                       => _x( 'Authors', 'taxonomy general name', 'textdomain' ),
		'singular_name'              => _x( 'Author', 'taxonomy singular name', 'textdomain' ),
		'search_items'               => __( 'Search Authors', 'textdomain' ),
		'popular_items'              => __( 'Popular Authors', 'textdomain' ),
		'all_items'                  => __( 'All Authors', 'textdomain' ),
		'parent_item'                => null,
		'parent_item_colon'          => null,
		'edit_item'                  => __( 'Edit Author', 'textdomain' ),
		'update_item'                => __( 'Update Author', 'textdomain' ),
		'add_new_item'               => __( 'Add New Author', 'textdomain' ),
		'new_item_name'              => __( 'New Author Name', 'textdomain' ),
		'separate_items_with_commas' => __( 'Separate authors with commas', 'textdomain' ),
		'add_or_remove_items'        => __( 'Add or remove authors', 'textdomain' ),
		'choose_from_most_used'      => __( 'Choose from the most used authors', 'textdomain' ),
		'not_found'                  => __( 'No authors found.', 'textdomain' ),
		'menu_name'                  => __( 'Authors', 'textdomain' ),
	);
	$args = array(
		'hierarchical'          => false,
		'labels'                => $labels,
		'show_ui'               => true,
		'show_admin_column'     => true,
		'update_count_callback' => '_update_post_term_count',
		'query_var'             => true,
		'rewrite'               => array( 'slug' => 'authors' ),
	);
	register_taxonomy( 'authors', ['post'], $args);
}


Displaying the Taxonomies

To display the hierarchical taxonomy (car models), add the following code to the page where you want it displayed:

<?php the_terms( $post->ID, 'car-models', 'Car Models: ', ', ', ' ' ); ?>

To display the non-hierarchical taxonomy (authors), add the following code to the page where you want it displayed:

<?php the_terms( $post->ID, 'author', 'Authors: ', ', ', ' ' ); ?-->

If you do not want to hardcode the displaying of the taxonomies, you can install the plugin List Custom Taxonomy Widget to display the taxonomies in the widget-ready areas of your WordPress site. To customize the order of the taxonomies, a good plugin for that is Custom Taxonomy Order.

If – for whatever reason – you want to get rid of the default Categories and Tags taxonomies from your WordPress site altogether, the article Removing the Categories and Tags Taxonomies in WordPress can help you. The beauty of creating custom taxonomies is you can use more descriptive words or terminologies that best suit the kind of website you are working on.