0
0
Wordpressframework~5 mins

Custom taxonomies in Wordpress

Choose your learning style9 modes available
Introduction

Custom taxonomies help you organize content in WordPress beyond default categories and tags. They let you group posts or items in ways that fit your site.

You want to group products by brand on an online store.
You need to organize blog posts by topic and difficulty level.
You want to classify events by location or type.
You want to add custom filters for recipes by cuisine or cooking time.
Syntax
Wordpress
register_taxonomy( string $taxonomy, array|string $object_type, array|string $args = array() )
Use register_taxonomy inside a function hooked to init action.
The $taxonomy is the name of your custom taxonomy (like 'genre').
Examples
This creates a simple taxonomy called 'genre' for blog posts.
Wordpress
function create_genre_taxonomy() {
  register_taxonomy('genre', 'post');
}
add_action('init', 'create_genre_taxonomy');
This creates a hierarchical taxonomy 'brand' for products, like categories.
Wordpress
function create_product_brand_taxonomy() {
  register_taxonomy('brand', 'product', [
    'label' => 'Brands',
    'hierarchical' => true
  ]);
}
add_action('init', 'create_product_brand_taxonomy');
This creates a non-hierarchical taxonomy 'event_type' for events and posts, like tags.
Wordpress
function create_event_type_taxonomy() {
  register_taxonomy('event_type', ['event', 'post'], [
    'label' => 'Event Types',
    'hierarchical' => false
  ]);
}
add_action('init', 'create_event_type_taxonomy');
Sample Program

This code creates a custom taxonomy called 'genre' for a custom post type 'movie'. It is hierarchical, so it works like categories. It also shows in the admin area for easy use.

Wordpress
<?php
function create_movie_genre_taxonomy() {
  $args = [
    'label' => 'Genres',
    'public' => true,
    'hierarchical' => true,
    'show_ui' => true,
    'show_admin_column' => true
  ];
  register_taxonomy('genre', 'movie', $args);
}
add_action('init', 'create_movie_genre_taxonomy');

// This code adds a 'genre' taxonomy for 'movie' post type.
?>
OutputSuccess
Important Notes

Always hook your taxonomy registration to the init action to ensure WordPress is ready.

Hierarchical taxonomies behave like categories (can have parent-child), non-hierarchical like tags (flat list).

Use descriptive labels to make your taxonomy clear in the admin dashboard.

Summary

Custom taxonomies help organize content in ways that fit your site.

Use register_taxonomy inside an init hook to create them.

Choose hierarchical or non-hierarchical depending on how you want to group items.