🏷️ More Than Categories and Tags
Posts have categories, tags. Products need brands, sizes, colors. Custom taxonomies create any classification system.
📝 Register Custom Taxonomy
// functions.php
function create_product_taxonomies() {
// Brand taxonomy (hierarchical like categories)
register_taxonomy('product_brand', 'product', array(
'labels' => array(
'name' => 'Brands',
'singular_name' => 'Brand',
'search_items' => 'Search Brands',
'all_items' => 'All Brands',
'parent_item' => 'Parent Brand',
'parent_item_colon' => 'Parent Brand:',
'edit_item' => 'Edit Brand',
'update_item' => 'Update Brand',
'add_new_item' => 'Add New Brand',
'new_item_name' => 'New Brand Name',
'menu_name' => 'Brands'
),
'hierarchical' => true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'brand'),
'show_in_rest' => true // Block editor support
));
// Size taxonomy (non-hierarchical like tags)
register_taxonomy('product_size', 'product', array(
'labels' => array(
'name' => 'Sizes',
'singular_name' => 'Size',
'search_items' => 'Search Sizes',
'popular_items' => 'Popular Sizes',
'all_items' => 'All Sizes',
'edit_item' => 'Edit Size',
'update_item' => 'Update Size',
'add_new_item' => 'Add New Size',
'new_item_name' => 'New Size Name',
'menu_name' => 'Sizes'
),
'hierarchical' => false,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'size'),
'show_in_rest' => true
));
}
add_action('init', 'create_product_taxonomies');
🎯 Display Taxonomy in Template
// Get terms for current post
$brands = get_the_terms(get_the_ID(), 'product_brand');
if ($brands && !is_wp_error($brands)) {
echo '<div class="product-brand">Brand: ';
foreach ($brands as $brand) {
echo '<a href="' . get_term_link($brand) . '">' . $brand->name . '</a>';
}
echo '</div>';
}
// Query products by brand
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_brand',
'field' => 'slug',
'terms' => 'nike'
)
)
);
$query = new WP_Query($args);
💡 Use Cases
- Products: Brands, Sizes, Colors, Materials
- Recipes: Cuisine, Diet, Difficulty, Cooking Time
- Movies: Genre, Director, Actor, Year
- Real Estate: Property Type, Location, Price Range
“Categories and tags weren’t enough for products. Added Brand taxonomy. Now customers can filter by brand. SEO improved because brand pages have unique content.”
