Understanding and creating custom Taxonomy

Custom Taxonomies in WordPress

Custom Taxonomies in WordPress

Taxonomies are a way of grouping similar items together. This makes content to be relational. By default WordPress comes with two taxonomies

  1. Category – Allows grouping of similar posts, categories are hierarchical
  2. Tags – Allows grouping of similar posts, not hierarchical. It’s kind of label to each post.

Each category or tag you create is a term in WordPress database. WordPress uses three database tables to store database information. They are wp_terms, wp_term_relationships, wp_term_taxonomy. 

  • wp_terms – This table stores all your term information
  • wp_term_taxonomy – This table stores the taxonomy for which the term belongs. It also stores parent of the term if the taxonomy is hirarchial.
  • wp_term_relationships – This table is a cross reference table for your content and terms. It stores post id of each content and its term id in this table.

Following is a query to show display all posts along with taxonomy terms assigned to each posts

 Building your own Taxonomies

Building custom taxonomies gives you the freedom of different methods of categorizing your content. This will make your WordPress a full pledged content management system(CMS) along with the usage of metadata for content. With each custom taxonomy WordPress creates a meta box for posts to add terms to new Taxonomy while creating a content. It also gives administrative panel for managing new custom taxonomies too. New taxonomies are created using register_taxonomy function. The syntax is as in the following

Here is how parameters are.

$taxonomy – Name of the taxonomy

$object_type – Content type for which taxonomy is associated. It can be array of object type names

$args – Optional values for custom taxonomy.

Remember , it is always a best practise to call this function using init action. Following example explains a simple way of creating custom taxonomy

In the above example custom taxonomy is registered using register_taxonomy function. The first parameter is name of the taxonomy which is ‘type’ . The second parameter is content type for which this taxonomy is attached, it is ‘products’ content type here. Third argument is optional values for taxonomy. The parameter hierarchical defines whether terms can be like a tree, label defines how taxonomy is identified, query_var  allows taxonomy to be queried in URL, rewrite allows pretty permalink to be setup for custom taxonomy.

After this you’ll see a admin menu for handling taxonomy ‘Types” just below the Products menu. Also you’ll see a metabox in ‘Products’ editing screen for adding categories in ‘Type’ taxonomy.

You can set a variety of custom options while creating a taxonomy in WordPress

  • public – It sets whether admin menu is available for custom taxonomy in dashboard and front page.
  • show_ui – It sets whether admin user interface is available in dashboard for managing custom taxonomy
  • show_in_nav_menus – Setting this true allows custom taxonomy to be available in navigation menus
  • show_tagcloud – Setting this true allows custom taxonomy to be available in tag cloud widget.
  • show_admin_column – Setting this true makes taxonomy columns to be created automatically in the associated post types table
  • hierarchical– Sets whether custom taxonomy is hierarchical
  • query_var – Enables custom taxonomy to be accessed by custom query variable in URL
  • rewrite – Allows using rewrite rules and setting pretty permalinks for this custom taxonomy
    • slug – Sets the custom pretty permalink slug
    • front_base – Sets whether custom taxonomy should use front base set in permalink settings
    • hierarchical  – Allows hierarchical URLs.
  • sort – Whether this taxonomy should remember the order in which terms are added to content.
  • labels – WordPress allows several strings of text to appear in admin dashboard for custom taxonomy. Following are the available labels for custom taxonomy
    • name – General name of the taxonomy. It’s usually plural
    • singular_name – Singular version of the name for the taxonomy
    • menu_name – The text name given to the menu
    • search_items – Text for search items button
    • popular_items – Label for popular items text
    • all_items – Label for all items text
    • parent_item – The label for parent item. It has no meaning if taxonomy is not hierarchical.
    • edit_item – It is used as a text string while editing individual taxonomy term
    • view_item – Used as view item text
    • update_item – Used as text for updating taxonomy term
    • add_new_item – Text for creating new taxonomy term
    • separate_items_with_commas – Separates terms with commas in taxonomy meta box. No meaning if taxonomy is hierarchical.
    • choose_from_most_used – The “choose from most used” text used in the taxonomy meta box. Not used on hierarchical taxonomies.
    • not_found – The text displayed when you click “Choose from the most used tags” and no tags are available.

Setting labels makes it easier while administrating custom taxonomy terms in the administrative menu.

Following is a example of custom taxonomy registration with labels.

Using your custom Taxonomy

WordPress features some easy to use functions for working with your custom taxonomy . The following function shows how you can display tag cloud for your custom taxonomy

Here first argument takes the name of the taxonomy and second argument takes number of terms to be displayed in the tag cloud.

You can create a custom loop using WP_Query object to retrieve content attached to your custom taxonomy.

The tax_query argument specifies which term and which taxonomy to use in retrieving posts for loop.

You can easily display custom taxonomy terms assigned to each post using following function

This function needs to be in the loop in your theme template file to work properly. To get terms for the post you need to send the post id , custom taxonomy name and title to be displayed net to the terms.

The function get_terms() can be used to retrieve terms for a particular custom taxonomy

The above code will retrieve all the terms associated with the taxonomy ‘type’.

Defining our own custom taxonomy is a powerful way of organizing website content. Using the above we can allow WordPress to handle complicated content to become a powerful Content Management System.

Leave a Reply