register_taxonomy

Home » Functions » register_taxonomy

One of the most powerful features of WordPress is its ability to organize content through taxonomies. Taxonomies are a way of grouping related content together, and they can be used for a wide variety of purposes. One of the most common taxonomies used in WordPress is the category taxonomy, which is used to group posts together based on their content. However, WordPress also allows developers to create their own custom taxonomies, which can be used to organize any type of content.

The register_taxonomy function is used to register a new custom taxonomy in WordPress. This function takes two parameters: the first parameter is the name of the taxonomy, and the second parameter is the post type (or types) to which it should be attached.

Here’s an example usage of the register_taxonomy function:

function my_custom_taxonomy() {
    $labels = array(
        'name'              => _x( 'My Taxonomy', 'taxonomy general name' ),
        'singular_name'     => _x( 'My Taxonomy', 'taxonomy singular name' ),
        'search_items'      => __( 'Search My Taxonomy' ),
        'all_items'         => __( 'All My Taxonomy' ),
        'parent_item'       => __( 'Parent My Taxonomy' ),
        'parent_item_colon' => __( 'Parent My Taxonomy:' ),
        'edit_item'         => __( 'Edit My Taxonomy' ),
        'update_item'       => __( 'Update My Taxonomy' ),
        'add_new_item'      => __( 'Add New My Taxonomy' ),
        'new_item_name'     => __( 'New My Taxonomy Name' ),
        'menu_name'         => __( 'My Taxonomy' ),
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
    );

    register_taxonomy( 'my_taxonomy', 'post', $args );
}
add_action( 'init', 'my_custom_taxonomy', 0 );

In this example, we’re registering a new custom taxonomy called "My Taxonomy" that’s attached to the "post" post type. We’re also defining a set of labels that will be used to describe the taxonomy in the WordPress admin interface. Finally, we’re passing all of this information to the register_taxonomy function to create the new taxonomy.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.