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.
Custom taxonomies in Wordpress
register_taxonomy( string $taxonomy, array|string $object_type, array|string $args = array() )
register_taxonomy inside a function hooked to init action.$taxonomy is the name of your custom taxonomy (like 'genre').function create_genre_taxonomy() {
register_taxonomy('genre', 'post');
}
add_action('init', 'create_genre_taxonomy');function create_product_brand_taxonomy() {
register_taxonomy('brand', 'product', [
'label' => 'Brands',
'hierarchical' => true
]);
}
add_action('init', 'create_product_brand_taxonomy');function create_event_type_taxonomy() {
register_taxonomy('event_type', ['event', 'post'], [
'label' => 'Event Types',
'hierarchical' => false
]);
}
add_action('init', 'create_event_type_taxonomy');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.
<?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.
?>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.
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.
Practice
Solution
Step 1: Understand what taxonomy means in WordPress
Taxonomies group content like categories or tags to organize posts.Step 2: Identify the purpose of custom taxonomies
Custom taxonomies let you create your own groups to better fit your content.Final Answer:
To organize content in a way that fits your website's needs -> Option DQuick Check:
Custom taxonomy = organize content [OK]
- Confusing taxonomies with themes or plugins
- Thinking taxonomies affect site speed
- Mixing taxonomies with user roles
genre for posts in WordPress?Solution
Step 1: Check the function signature of register_taxonomy()
The function requires taxonomy name, object type(s) as string or array, and an array of arguments.Step 2: Verify correct parameter types and syntax
register_taxonomy('genre', 'post', array('hierarchical' => true)); uses 'post' as string and an array for arguments correctly.Final Answer:
register_taxonomy('genre', 'post', array('hierarchical' => true)); -> Option AQuick Check:
Correct syntax uses array for args and string for post type [OK]
- Using plural 'posts' instead of 'post'
- Passing arguments outside an array
- Wrong parameter order
register_taxonomy('genre', 'post', array('hierarchical' => true));
$terms = get_terms(array('taxonomy' => 'genre', 'hide_empty' => false));
foreach ($terms as $term) {
echo $term->name . ', ';
}What will this code output if there are two terms 'Fiction' and 'Non-Fiction' assigned to posts?
Solution
Step 1: Understand get_terms() usage
get_terms() fetches terms of the taxonomy 'genre' including empty ones due to 'hide_empty' => false.Step 2: Loop through terms and print their names
The foreach loop prints each term's name followed by a comma and space.Final Answer:
Fiction, Non-Fiction, -> Option BQuick Check:
Loop prints term names separated by commas [OK]
- Expecting taxonomy name instead of term names
- Confusing array output with string
- Not setting 'hide_empty' causing missing terms
register_taxonomy('genre', 'post', 'hierarchical' => true);Solution
Step 1: Check the function parameters
register_taxonomy() expects the third argument as an array of arguments.Step 2: Identify the error in argument format
The code passes 'hierarchical' => true directly, not inside an array, causing syntax error.Final Answer:
The third argument should be an array, not a key-value pair directly -> Option CQuick Check:
Args must be in array() [OK]
- Passing arguments without array()
- Using plural taxonomy names incorrectly
- Wrong function name like register_taxonomies
location that behaves like tags (non-hierarchical) and attach it to both post and event post types. Which code snippet correctly achieves this?Solution
Step 1: Understand hierarchical argument for tags
Tags are non-hierarchical, so 'hierarchical' must be false.Step 2: Attach taxonomy to multiple post types
Pass an array of post types ['post', 'event'] as second argument.Step 3: Check options for correct syntax and behavior
register_taxonomy('location', array('post', 'event'), array('hierarchical' => false)); correctly sets 'hierarchical' => false and attaches to both post types in one call.Final Answer:
register_taxonomy('location', array('post', 'event'), array('hierarchical' => false)); -> Option AQuick Check:
Non-hierarchical + multiple post types = register_taxonomy('location', array('post', 'event'), array('hierarchical' => false)); [OK]
- Setting hierarchical true for tags
- Calling register_taxonomy twice unnecessarily
- Passing post types as string instead of array
Ready to prove it?
Pass the test and this pattern moves unknown
