Last updated on September 13, 2023

Register Custom Taxonomy

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Register a custom taxonomy.

To register a custom taxonomy in WordPress, you can use the register_taxonomy() function. This allows you to create a new taxonomy, such as a category or tag-like classification, for your custom post types. Here’s an example of how you can register a custom taxonomy:

function wpsnippets_register_custom_taxonomy() {
    $labels = array(
        'name'                       => _x( 'Custom Taxonomy', 'taxonomy general name', 'text-domain' ),
        'singular_name'              => _x( 'Custom Taxonomy', 'taxonomy singular name', 'text-domain' ),
        'search_items'               => __( 'Search Custom Taxonomy', 'text-domain' ),
        'popular_items'              => __( 'Popular Custom Taxonomy', 'text-domain' ),
        'all_items'                  => __( 'All Custom Taxonomy', 'text-domain' ),
        'edit_item'                  => __( 'Edit Custom Taxonomy', 'text-domain' ),
        'update_item'                => __( 'Update Custom Taxonomy', 'text-domain' ),
        'add_new_item'               => __( 'Add New Custom Taxonomy', 'text-domain' ),
        'new_item_name'              => __( 'New Custom Taxonomy Name', 'text-domain' ),
        'separate_items_with_commas' => __( 'Separate items with commas', 'text-domain' ),
        'add_or_remove_items'        => __( 'Add or remove items', 'text-domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'text-domain' ),
        'not_found'                  => __( 'No custom taxonomy found', 'text-domain' ),
        'menu_name'                  => __( 'Custom Taxonomy', 'text-domain' ),
    );

    $args = array(
        'labels'                     => $labels,
        'public'                     => true,
        'hierarchical'               => true, // Change to false if non-hierarchical
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );

    register_taxonomy( 'custom_taxonomy', array( 'post' ), $args );
}

add_action( 'init', 'wpsnippets_register_custom_taxonomy', 0 );

In this example, the custom taxonomy is named “Custom Taxonomy”. You can customize the taxonomy labels and settings according to your needs.

Inside the wpsnippets_register_custom_taxonomy() function, the $labels array defines the labels for the taxonomy. Adjust the labels as desired. The $args array contains the configuration options for the taxonomy itself. You can modify the options such as public, hierarchical, and others to suit your requirements. The register_taxonomy() function is called with the taxonomy slug (custom_taxonomy), the post types to which the taxonomy is applicable (in this case, ‘post’), and the $args array.

By adding this code snippet to your site, you can register a custom taxonomy in WordPress, and you can associate it with your custom post types or use it for standard posts.

Last updated on September 13, 2023. Originally posted on June 22, 2023.

Leave a Reply