Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I add a custom post type with hierarchical categories in WordPress?
Asked on Feb 19, 2026
Answer
To add a custom post type with hierarchical categories in WordPress, you need to register both the custom post type and a custom taxonomy that supports hierarchy. This can be done using the `register_post_type` and `register_taxonomy` functions in your theme's `functions.php` file or a custom plugin.
<!-- BEGIN COPY / PASTE -->
function create_custom_post_type() {
// Register Custom Post Type
register_post_type('custom_post', array(
'labels' => array(
'name' => __('Custom Posts'),
'singular_name' => __('Custom Post')
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail'),
));
// Register Custom Taxonomy
register_taxonomy('custom_category', 'custom_post', array(
'hierarchical' => true,
'labels' => array(
'name' => __('Custom Categories'),
'singular_name' => __('Custom Category')
),
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'custom-category'),
));
}
add_action('init', 'create_custom_post_type');
<!-- END COPY / PASTE -->Additional Comment:
- Ensure you have the correct capabilities set if you need specific user roles to manage these custom post types and taxonomies.
- After adding this code, you may need to refresh your permalinks by visiting Settings → Permalinks in the WordPress dashboard.
- Consider using a child theme or a custom plugin to avoid losing changes during theme updates.
Recommended Links:
