Ask any question about WordPress here... and get an instant response.
How can I create a custom post type with its own taxonomy?
Asked on Nov 06, 2025
Answer
Creating a custom post type with its own taxonomy in WordPress involves using the `register_post_type` and `register_taxonomy` functions. These functions allow you to define new content types and organize them with custom taxonomies.
<!-- BEGIN COPY / PASTE -->
function my_custom_post_type() {
$args = array(
'public' => true,
'label' => 'Books',
'supports' => array('title', 'editor', 'thumbnail'),
);
register_post_type('book', $args);
}
add_action('init', 'my_custom_post_type');
function my_custom_taxonomy() {
$args = array(
'label' => 'Genres',
'rewrite' => array('slug' => 'genre'),
'hierarchical' => true,
);
register_taxonomy('genre', 'book', $args);
}
add_action('init', 'my_custom_taxonomy');
<!-- END COPY / PASTE -->Additional Comment:
- The `register_post_type` function is used to create a new post type, in this case, "Books".
- The `register_taxonomy` function creates a new taxonomy, "Genres", associated with the "Books" post type.
- Place this code in your theme's `functions.php` file or a custom plugin.
- Ensure your theme supports custom post types and taxonomies for proper display.
Recommended Links:
