taxonomies in wordpress

Taxonomies in WordPress. What are they for? How are they created?

Have we ever wondered how we can create new classifications (taxonomies) in WordPress. For example, you could have a movie site and there is a term that classifies them by genre (Drama, Terror or Adventures). I could have a portfolio to show my works and want to filter them by a classification that identifies them as: Type of Portfolio (Responsive Web, E-commerce or Web Business), for this purpose there are taxonomies and in this post we will detail them.

What is a taxonomy?

The word taxonomy comes from Greece and is used for the process of classification or ordering of objects, meanings, types of knowledge, etc. In Greek taxo means ordering and organizations; So it is easy to deduce that it is a taxonomy, but if you do not understand it yet, just think of it as something to group together certain elements that have something in common.

What is a taxonomy in WordPress?

After getting an idea of ​​what taxonomy means, taking it to the WordPress context is easy. WordPress you use them to sort and group post_type, either those that come by defects such as page and post, or those that we define through the Custom Post Type (CPT), by the way if you don’t know what a Custom Post Type is, read this article about them:

Hierarchical and non-hierarchical taxonomies

There are two types of taxonomies in WordPress: hierarchical and non-hierarchical. The hierarchical ones are like the categories that we find in a basic WordPress installation, they support a parent-child structure, which is nothing more than a category can have sub-categories, for example the Animal category can have sub-categories like: Mammals , Birds and Reptiles, even Birds can have sub-categories such as: Turkeys, Chickens or Canaries and so on forming a tree structure. Non-hierarchical ones are simpler and more linear like WordPress tags and are mostly used to classify objects without having to depend on a parent element, for example the colors Blue, Green or Yellow.

How to create a taxonomy in WordPress?

To create a taxonomy in WordPress is simple, it’s just two steps.

1 – Create a function that will define the taxonomy.

add_action('init', 'wr_portfolio_taxonomies');

This function has two variables inside $labels, $args and a register_taxonomy() function. The $labels variable is an array that has all the taxonomy customization tags.

$args :It has a set of properties such as: hierarchical that defines whether the taxonomy will be hierarchical or not; labels property receiving the $labels tag array; show_admin_colum defines if a column will be shown in the table of the post_type that make up the taxonomy; rewrite defines the post_type that will be classified by the taxonomy.

2- Call the hook that creates the taxonomy.

function wr_portfolio_taxonomies(){
    
    $labels = array(
        'name' => 'Types Portfolio',
        'singular_name' => 'Type Portfolio',
        'search_items' => 'Search Types Portfolio',
        'all_items' => 'All Types Portfolio',
        'parent_item' => 'Parent Type Portfolio',
        'parent_item_colon' => 'Parent Type Portfolio:',
        'edit_item' => 'Edit Type Portfolio',
        'update_item' => 'Update Type Portfolio',
        'add_new_item' => 'Add New Type Portfolio',
        'new_item_name' => 'New Type Portfolio Name',
        'menu_name' => 'Types Portfolio'
    );

    $args = array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'type_portfolio')
    );

    register_taxonomy( 'type_portfolio', array('portfolio'), $args );
}

You just need to call the init action to complete the registration process of the new taxonomy.

Conclusion

As we have seen taxonomies play a fundamental role in WordPress, its use greatly enhances this technology. Taxonomies make this CMS (Content Management System) not only a blog, but what your mind can create. We can create hotel reservation sites, websites that create events and even as a strong competition to develop virtual stores. Check more about taxonomies in WordPress Official Doc.

If you want to keep learning about WordPress development, read this articles:

Hooks in WordPress

Custom Post Type in WordPress (CPT)

How to create a plugin in WordPress?