Why does a new taxonomy term get created when I assign an existing term to a post?

admin2025-06-05  1

I've got a stumper... I've created a new taxonomy called Article Type. I created four values for it, as shown in this metabox:

When I select the "Entertainment" term and update the post, something weird happens: the system creates a new term using the term_id from Entertainment in table wp_terms, see pix below:

This is clearly not the desired effect, I'm just trying to select "Entertainment".

I have no idea where to start digging into this...can anyone tell me why this is happening?

In case it matters, here's the code used to create the taxonomy:

function hhl_article_type() {
    $labels = array(
        'name'                       => _x( 'Types', 'Taxonomy General Name', 'hhl' ),
        'singular_name'              => _x( 'Type', 'Taxonomy Singular Name', 'hhl' ),
        'menu_name'                  => __( 'Article types', 'hhl' ),
        'all_items'                  => __( 'All Items', 'hhl' ),
        'parent_item'                => __( 'Parent Item', 'hhl' ),
        'parent_item_colon'          => __( 'Parent Item:', 'hhl' ),
        'new_item_name'              => __( 'New Item Name', 'hhl' ),
        'add_new_item'               => __( 'Add New Item', 'hhl' ),
        'edit_item'                  => __( 'Edit Item', 'hhl' ),
        'update_item'                => __( 'Update Item', 'hhl' ),
        'view_item'                  => __( 'View Item', 'hhl' ),
        'separate_items_with_commas' => __( 'Separate items with commas', 'hhl' ),
        'add_or_remove_items'        => __( 'Add or remove items', 'hhl' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'hhl' ),
        'popular_items'              => __( 'Popular Items', 'hhl' ),
        'search_items'               => __( 'Search Items', 'hhl' ),
        'not_found'                  => __( 'Not Found', 'hhl' ),
        'no_terms'                   => __( 'No items', 'hhl' ),
        'items_list'                 => __( 'Items list', 'hhl' ),
        'items_list_navigation'      => __( 'Items list navigation', 'hhl' ),
    );
    $rewrite = array(
        'slug'                       => 'type',
        'with_front'                 => true,
        'hierarchical'               => false,
    );
    $args = array(
        'labels'              => $labels,
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_admin_column'   => true,
        'show_in_nav_menus'   => true,
        'show_tagcloud'       => false,
        'rewrite'             => $rewrite,
        'show_in_rest'        => false,
        'meta_box_cb'         => 'post_categories_meta_box',
    );
    register_taxonomy( 'article_type', array( 'post' ), $args );
}
add_action( 'init', 'hhl_article_type', 0 );

I've got a stumper... I've created a new taxonomy called Article Type. I created four values for it, as shown in this metabox:

When I select the "Entertainment" term and update the post, something weird happens: the system creates a new term using the term_id from Entertainment in table wp_terms, see pix below:

This is clearly not the desired effect, I'm just trying to select "Entertainment".

I have no idea where to start digging into this...can anyone tell me why this is happening?

In case it matters, here's the code used to create the taxonomy:

function hhl_article_type() {
    $labels = array(
        'name'                       => _x( 'Types', 'Taxonomy General Name', 'hhl' ),
        'singular_name'              => _x( 'Type', 'Taxonomy Singular Name', 'hhl' ),
        'menu_name'                  => __( 'Article types', 'hhl' ),
        'all_items'                  => __( 'All Items', 'hhl' ),
        'parent_item'                => __( 'Parent Item', 'hhl' ),
        'parent_item_colon'          => __( 'Parent Item:', 'hhl' ),
        'new_item_name'              => __( 'New Item Name', 'hhl' ),
        'add_new_item'               => __( 'Add New Item', 'hhl' ),
        'edit_item'                  => __( 'Edit Item', 'hhl' ),
        'update_item'                => __( 'Update Item', 'hhl' ),
        'view_item'                  => __( 'View Item', 'hhl' ),
        'separate_items_with_commas' => __( 'Separate items with commas', 'hhl' ),
        'add_or_remove_items'        => __( 'Add or remove items', 'hhl' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'hhl' ),
        'popular_items'              => __( 'Popular Items', 'hhl' ),
        'search_items'               => __( 'Search Items', 'hhl' ),
        'not_found'                  => __( 'Not Found', 'hhl' ),
        'no_terms'                   => __( 'No items', 'hhl' ),
        'items_list'                 => __( 'Items list', 'hhl' ),
        'items_list_navigation'      => __( 'Items list navigation', 'hhl' ),
    );
    $rewrite = array(
        'slug'                       => 'type',
        'with_front'                 => true,
        'hierarchical'               => false,
    );
    $args = array(
        'labels'              => $labels,
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_admin_column'   => true,
        'show_in_nav_menus'   => true,
        'show_tagcloud'       => false,
        'rewrite'             => $rewrite,
        'show_in_rest'        => false,
        'meta_box_cb'         => 'post_categories_meta_box',
    );
    register_taxonomy( 'article_type', array( 'post' ), $args );
}
add_action( 'init', 'hhl_article_type', 0 );
Share Improve this question asked Dec 7, 2018 at 1:06 scott8035scott8035 2302 silver badges10 bronze badges 3
  • 1 Your taxonomy is non-hierarchical, but you have used the callback for hierarchical taxonomies. – Milo Commented Dec 7, 2018 at 3:35
  • Milo what does that mean in laymen terms? – Pete Commented Dec 7, 2018 at 5:45
  • The meta_box_cb argument is incorrect, read the description for that parameter on the documentation page. Unless you are setting a specific value, it's best to remove it, along with all the other arguments that are set to default values. – Milo Commented Dec 7, 2018 at 16:11
Add a comment  | 

1 Answer 1

Reset to default 0

Thanks to @Milo for the solution. I deleted all the defaults to condense the code, and then changed the 'meta_box_cb' parameter to 'post_categories_meta_box', and it did indeed give me a tag-style metabox.

As an aside, that's not exactly what I envisioned, so I ended up using CMB2's taxonomy_radio field to get what I was after (a drop-down would have worked too):

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749118881a316522.html

最新回复(0)