$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Why does a new taxonomy term get created when I assign an existing term to a post?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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):

Post a comment

comment list (0)

  1. No comments so far