Create Custom Taxonomies in WordPress

A WordPress Taxonomy is used to organize content like posts, pages or custom post types. WordPress supports by default a builtin taxonomy "Category" for post type "Post". In this post we will register a custom taxonomy without using a plugin.

Create Custom Taxonomies in Wordpress

In WordPress a taxonomy is like a group for your post type which you can assign. Think of it as a category for a product. We created a custom post type "Property" in our post Create Custom Post Types in WordPress. Today we will create a custom taxonomy "City" and assign it to property. 


What Are Custom Taxonomies in WordPress

A taxonomy allows to classify content in WordPress. WordPress includes built-in taxonomies such as  "Category" and "Tag", but it also allows developers to register custom taxonomies for any custom post types for better organization of custom post types.

The WordPress function used to register a custom taxonomy is:
register_taxonomy( $slug, $post_types, $args );

  • $slug (string): A unique slug for our taxonomy i.e. "property-type".
  • $post_types (array) : An array post type slugs for which these taxonomy will be registered.
  • $args (array): An array of arguments for this custom taxonomy containing label strings and other configuration options.

For complete understanding and example of this function visit WordPress Register Taxonomy page. 

 

How to Register a Custom Taxonomy in WordPress

We will first create a file custom_taxonomies.php in which we will use the function register_taxonomy() to create a custom taxonomy for a custom post type. Then we will include this file in functions.php file. The following code snippet registers a custom taxonomy called property-city for a property custom post type. The configuration for our taxonomy are defined in $labels and $args variables.

custom_taxonomies.php

<?php 
add_action('init', 'theme_taxonomies');
function theme_taxonomies() {
// --- Labels Array --- //

// General name for the taxonomy, usually plural
$labels['name'] = __('City', 'text-domain');

// Name for one object of this taxonomy
$labels['singular_name'] = __('City', 'text-domain');

// Taxonomy name in menu
$labels['menu_name'] = __('Cities', 'text-domain');

// Label to signify all items in a submenu link
$labels['all_items'] = __('All Cities', 'text-domain');

// Label for adding a new term item button on list screen
$labels['add_new_item'] = __('Add New City', 'text-domain');

// Label for link to add a new term item
$labels['new_item_name'] = __('New City', 'text-domain');

// Label for link to edit term item
$labels['edit_item'] = __('Edit City', 'text-domain');

// Label for link to view term
$labels['view_item'] = __('View City', 'text-domain');

// Label for link to update a term item
$labels['update_item'] = __('Update City', 'text-domain');

// Label for search item button
$labels['search_items'] = __('Search City', 'text-domain');

// Label for text when no term is found
$labels['not_found'] = __('No City Found', 'text-domain');

// The labels array we prepare above
$args['labels'] = $labels;

// Whether a taxonomy is intended for use publicly
$args['public'] = true;

// Whether the taxonomy is publicly queryable
$args['publicly_queryable'] = true;

// Whether the taxonomy is hierarchical
$args['hierarchical'] = false;

// Whether to show the taxonomy in the admin menu
$args['show_in_menu'] = true;

// Sets the query var key for this taxonomy
$args['query_var'] = true;

// Triggers the handling of rewrites for this taxonomy
$args['rewrite'] = [ 'slug' => 'property-city' ];

// Wordpress function to register taxonomy
register_taxonomy('property-city', ['property'], $args);
}
And that is it, we just registered a custom taxonomy in WordPress for a custom post type. Now all we need to do is include custom_taxonomies.php in functions.php file.
<?php include('custom_taxonomies.php');?>

We now have a working custom taxonomy in WordPress that groups custom types with user-defined terms like "City". This taxonomy will now appear in admin area of WordPress and can be used in queries and templates.