Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I create a custom post type with hierarchical taxonomy in WordPress?
Asked on Feb 09, 2026
Answer
Creating a custom post type with a hierarchical taxonomy in WordPress involves registering both the post type and the taxonomy using WordPress functions. This allows you to organize content in a structured manner, similar to how pages and categories work.
<!-- BEGIN COPY / PASTE -->
function my_custom_post_type() {
register_post_type('book', array(
'labels' => array(
'name' => __('Books'),
'singular_name' => __('Book')
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail'),
));
}
add_action('init', 'my_custom_post_type');
function my_custom_taxonomy() {
register_taxonomy('genre', 'book', array(
'labels' => array(
'name' => __('Genres'),
'singular_name' => __('Genre')
),
'hierarchical' => true,
));
}
add_action('init', 'my_custom_taxonomy');
<!-- END COPY / PASTE -->Additional Comment:
- Ensure the code is added to your theme's functions.php file or a custom plugin.
- The 'hierarchical' parameter in register_taxonomy() allows the taxonomy to behave like categories.
- After registration, you can manage your custom post types and taxonomies from the WordPress dashboard.
- Always back up your site before making changes to functions.php.
Recommended Links:
