Create Custom Taxonomies in Wordpress

Taxonomy is like a category for your custom post type. By default wordpress comes with 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 create different 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 );

The first Parameter is slug of taxonomy, second parameter is array of post types this taxonomy will be linked to and third parameter is array of arguments for this taxonomy.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');?>