I was wondering what the use of a custom taxonomy is. While reading through several tutorials on how to create a custom post type I very often read about custom taxonomies. But I can’t really figure out what the concept behind that is, because categories can also be used in custom post types, so why use a custom taxonomy? When is it better to use custom taxonomies over categories. Can anyone explain the concept behind custom taxonomies?
4 Answers
Taxonomy
Essentially, a taxonomy is a way of classifying data. Typically a taxonomy will have a set of characteristics that is unique to it.
In WordPress by default it has four taxonomies, post tag, categories, link categories,Post Formats. To be obvious, if a taxonomy had the same characteristics as another data type, it wouldn’t be a different taxonomy! Example of those characteristics follow:
- Post Tag: acts like a label, attached to a post.
- Category: acts like a “bucket” in which we put posts, are often hierarchical. Posts can live in multiple categories.
- Link Category: acts like a label, attached to a link.
- Post Formats: data represent of a post and can be used by theme.
Official Documentation https://codex.wordpress.org/Taxonomies
https://github.com/WordPress/WordPress/blob/master/wp-includes/taxonomy.php#L24-L150
-
2For the sake of completeness there is a fifth native
nav-menu
taxonomy. It’s part of rather convoluted menus implementation and isn't really directly manipulated by user, but as such is an example that not all taxonomies are user–facing.– RarstCommented Nov 25, 2016 at 14:23
The concept behind custom taxonomies is to separate or create different types of categories for a one or more post-types. Some terms and post type are revealant only for a certain type of data.
A simple example would be if your website is a blog and a shop.
The custom post-type "product" has a custom category that will not be mixed with blog post categories. The post category taxonomy will not be available on product edit page, and the product category will not for the post post-type. A special template can be load if a certain taxonomy is queried.
All these behaviours can be change and set via filter and action depending on the arguments you pass through the required functions.
You typically define a custom taxonomy via this function:
function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
Here are the parameters:
* @param string $taxonomy Taxonomy key, must not exceed 32 characters.
* @param array|string $object_type Name of the object type for the taxonomy object.
* @param array|string $args {
* Optional.
The first parameter often referred as taxonomy name. The second parameter, you need to set where these taxonomies should be applied. The second parameter may be object type or an array of object types. Typically these will be post types or custom post types you have. But you can even leave it blank '' and define later when you create custom post types what taxonomies should be used.
The third parameter you may leave blank and in that case the defaults will be used. The defaults are at the moment.
$defaults = array(
'labels' => array(),
'description' => '',
'public' => true,
'publicly_queryable' => null,
'hierarchical' => false,
'show_ui' => null,
'show_in_menu' => null,
'show_in_nav_menus' => null,
'show_tagcloud' => null,
'show_in_quick_edit' => null,
'show_admin_column' => false,
'meta_box_cb' => null,
'capabilities' => array(),
'rewrite' => true,
'query_var' => $taxonomy,
'update_count_callback' => '',
'_builtin' => false,
);
Here is the example:
register_taxonomy(
'people',
array ('post', 'your_custom_post_type'),
array(
'label' => __( 'People' ),
'rewrite' => array( 'slug' => 'person' ),
'capabilities' => array(
'assign_terms' => 'edit_guides',
'edit_terms' => 'publish_guides'
)
)
);
Now from the four of the major custom taxonomy types, I think post tags and post categories are frequently used.
Post Tag: acts like a label, attached to a post.
Category: acts like a "bucket" in which we put posts, are often hierarchical. Posts can live in multiple categories.
Link Category: acts like a label, attached to a link.
Post Formats: data represent of a post and can be used by the theme.
The other two not that frequently. For instance, post formats were previously used in themes (twentysixteen) but twentyseventeen will not have post formats. Happens that they are not that attractive.
Finally, defining the custom taxonomy is only 1/2 of the job. The other half in order to use the taxonomy from custom post type.
register_post_type(
'your_custom_post_type'
,array(
'taxonomies' => array( 'people' )
// other arguments
)
);
I took the last example from this post.
"Custom taxonomy" is simple a wordpress plugins which we use for different purpose like if we add some Featured Images for both categories & tags, and display in widget or shortcode on your WordPress site, and many others post etc.