How can I register a custom taxonomy in WordPress?
Asked on Sep 05, 2025
Answer
To register a custom taxonomy in WordPress, you can use the `register_taxonomy` function within your theme's `functions.php` file or a custom plugin. This function allows you to create a new taxonomy and associate it with one or more post types.
<!-- BEGIN COPY / PASTE -->
add_action('init', 'create_custom_taxonomy');
function create_custom_taxonomy() {
$labels = array(
'name' => _x('Genres', 'taxonomy general name'),
'singular_name' => _x('Genre', 'taxonomy singular name'),
'search_items' => __('Search Genres'),
'all_items' => __('All Genres'),
'parent_item' => __('Parent Genre'),
'parent_item_colon' => __('Parent Genre:'),
'edit_item' => __('Edit Genre'),
'update_item' => __('Update Genre'),
'add_new_item' => __('Add New Genre'),
'new_item_name' => __('New Genre Name'),
'menu_name' => __('Genre'),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'genre'),
);
register_taxonomy('genre', array('post'), $args);
}
<!-- END COPY / PASTE -->Additional Comment:
- The `register_taxonomy` function should be hooked to the `init` action to ensure it runs at the correct time.
- Customize the `$labels` and `$args` arrays to fit the specific needs of your taxonomy.
- Ensure that the taxonomy slug is unique and does not conflict with existing taxonomies or post types.
- Test the taxonomy in your WordPress admin to ensure it appears as expected under the associated post types.
Recommended Links: