Create Custom Taxonomies in WordPress

Taxonomy is like a category for your post type. WordPress supports by default a builtin taxonomy "Category" for "Post". In this post we will register a custom taxonomy.

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. 


Custom Taxonomies in WordPress

WordPress has two built-in taxonomies "Category" and "Tag". But it still allows you to register custom taxonomies for your 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. 


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 our custom taxonomy. Then we will include this file in functions.php.

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, you 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.
<?php include('custom_taxonomies.php');?>