Creating Custom Taxonomies in WordPress

This time to build or create your first custom taxonomy! In this article you are going to create a simple taxonomy for defining types for your products. You have a ecommerce site and you are selling online products, then you obviously need a group that has specific products, Let’s suppose A Web Agency London selling its product and it has two groups one is the related with the design and the second is related with the development so both have different group. Here you are set up a custom taxonomy to define each type of Product in WordPress. The first thing use for create custom taxonomy is using the register_taxonomy() WordPress function it define your new taxonomy. The purpose of this function is allows you to customize how your new taxonomy will work and look.
Below the following code would work in a custom plugin, for this example, the functions.php file in your theme folder you’ll use. Now open up the functions.php in your theme and add the following code:

<?php
add_action( ‘init’, ‘prowp_define_product_type_taxonomy’ );
function prowp_define_product_type_taxonomy() {
register_taxonomy(‘type’,’product’,array(‘hierarchical’=>true,’label’=>’Type’,’query_var’=>true,’rewrite’=>true));
}
?>

It is first starts by calling the init hook, which tells WordPress to execute your prowp_define_product_type_taxonomy() function during initialization. Then calls the WordPress function register_taxonomy(). The purpose of this function is used to create your custom taxonomy based on what values you send.

Now break down the parameters that are sending in the register_taxonomy() function. First parameter is the taxonomy name, in this case type. This is the name that will define this taxonomy in the database. Second parameter is the object type. In this example, the name of the custom post is product. And the third parameter is for arguments, it means that you actually send multiple values to this parameter.

The above example there is four arguments passing. First on is hierarchical, it defines that your custom taxonomy can support nested taxonomies whether or not, forming a hierarchy. The above example, this is set as true, now your taxonomy function is just like WordPress’ that is built-in categories that may contain sub-categories. Next argument, label, it is used to set the name of your custom taxonomy for use in admin pages within WordPress.

The query_var argument is set to the false, it means that no queries can be made against the taxonomy;
And if you set this true then the taxonomy name with dashes replacing spaces is used as a query variable in URL strings. A string value for the query_var overrides the default. Let’s suppose, query_var => ‘strength’ would permit URL strings of the form example.com/?strength =weapons to be used to select content from the custom taxonomy.

Last and the final argument `use for rewrite, and this is set as true. It means that in WordPress tells whether or not you want a pretty permalink when viewing your custom taxonomy in the WordPress. If you set as true then you can access your custom taxonomy posts such as example.com/type/webdesign it looking good rather than the ugly method of example.com/?type=webdesign.

In this example you have created your custom taxonomy for type, now let’s see at what WordPress has done with your new taxonomy. You noticed in the admin panel dashboard that on the left side under the Products menu for your taxonomy labelled type has created. as shown in Figure below.

Clicking this new menu item brings you to the custom taxonomy admin panel for types, shown in figure below.
So this admin panel works exactly done work as the post categories admin panel do work. New taxonomy terms here you can create, deletes and edits existing terms, also search taxonomy terms and find how many products are assigned to each.

The new item added is a meta box on the product edit screen for your custom taxonomy, shown in Figure below.

For view this taxonomy, click on Add New Product. On the right side of your screen the meta box appears and it looks very similar to the Category meta box. The new types of your products you can easily add and delete here. You can set a variety of different arguments with custom post types when registering a custom taxonomy:

  •  public — This is use for sets whether a custom taxonomy is publicly available on the admin dashboard or front-end of your  website of WordPress. This is set to true by default. The default settings for show_in_nav_menus and show_ui are inherited  from this setting.
  •  show_ui — This is use for sets whether to create a default UI in the WordPress admin dashboard for managing this  taxonomy. Defaults to the value defined by the public argument.
  •  show_in_nav_menus — This is use for sets whether the post type is available for selection in the menu management feature  of WordPress. The value defined by the public argument by defaults.
  •  show_tagcloud — This is use for sets whether to allow the built-in Tag Cloud widget to use this taxonomy in WordPress.  Defaults to the value defined by the show_ui argument.
  •  hierarchical — This is use for sets whether this custom taxonomy is hierarchical like the categories or it is not hierarchical  like tags. This argument is set to false by default.
  •  update_count_callback —When a term in your taxonomy gets a count update then function name that will be called. The  default value is none.
  •  query_var — This is use for enables the public query var for the taxonomy. Acceptable values are false, true, or a string to  set a custom query var value.
  •  rewrite — This is use for the rewrite argument sets the URL parsing rules for permalinks referring to this taxonomy. You to customize the taxonomy slug in your URL allowed by this. This argument can be set to false, true, or an array of values. It accept the following values If an array is passing, by default this argument is set to true and the $taxonomy name is used as the slug.
  •  slug — This is use for set a custom permalink as slug. Defaults to the taxonomy name value.
  •  with_front — It is use for sets whether your taxonomy should use the front base from your permalink settings. Let’s suppose, you have set the prefixed your permalinks with /webdesign, and with_front is set to true, then at the beginning your taxonomy permalinks would include/webdesign.
  •  hierarchical — This allow hierarchical URLs. It is set false defaults. For more learn about the register_taxonomy() function then visit the official Codex page at this linkwww.codex.wordpress.org/Function_Reference/register_taxonomy// this is the official site of WordPress .