Create custom taxonomy in wordpress
To create a custom taxonomy in WordPress, you can use the register_taxonomy() function. Here are the steps to create a custom taxonomy:
- Create a new plugin or add the code to your theme’s functions.php file.
- Use the register_taxonomy() function to create a new custom taxonomy.
function create_custom_taxonomy() {
$args = array(
'label' => __( 'Custom Taxonomy' ),
'rewrite' => array( 'slug' => 'custom_taxonomy' ),
'hierarchical' => true,
);
register_taxonomy( 'custom_taxonomy', 'custom_post_type', $args );
}
add_action( 'init', 'create_custom_taxonomy' );
3. Replace ‘custom_taxonomy’ in the code with the desired name for your custom taxonomy.
4. Replace ‘Custom Taxonomy’ in the ‘label’ with the desired name for your custom taxonomy in the WordPress admin.
5. Replace ‘custom_post_type’ with the post type name you want to associate the custom taxonomy with.
6. Save the changes and activate the plugin or refresh your site.
The custom taxonomy will now be available in the WordPress admin under the “Posts” menu and will be associated with the post type you specified. It is recommended to test the changes on a development environment before pushing it to live site and also take a backup of your site before making any changes.