Create Custom Taxonomies in WordPress

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

Create Custom Taxonomies in Wordpress

Custom Taxonomies

WordPress allows you to register custom taxonomies for your custom post types. Custom taxonomy in WordPress is like a group for your specific posts. Let's say we have a custom post type "products" so we can register multiple custom taxonomies like "Mobiles", "Computers", "Laptops" etc. In this post we will register a custom taxonomy "Property type" for our custom post type "Properties".

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.

We will first create a file custom_taxonomies.php which will contain the code to register this new taxonomy and we include this file in our 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'] = __('Property types', 'text-domain');

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

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

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

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

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

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

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

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

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

// Label for text when no term is found
$labels['not_found'] = __('No Property type 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-type' ];

// Wordpress function to register taxonomy
register_taxonomy('property-type', ['property'], $args);
}

Now we include custom_taxonomies.php in functions.php
<?php include('custom_taxonomies.php');?>