2 different taxonomy fields save the value of the last field

Support Meta Box AIO 2 different taxonomy fields save the value of the last field

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #43174
    Alessandro LioceAlessandro Lioce
    Participant

    Hello! I added 2 metaboxes to the product category of woocommerce.

    Sample code

    add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
    	$meta_boxes[] = array(
    		'title'      => __( 'Box 1', 'lang' ),
    		'taxonomies' => 'product_cat',
    		'fields'     => array(
    			[
    				'type'       => 'taxonomy',
    				'name'       => esc_html__( 'Category', 'lang' ),
    				'id'         => 'field_1',
    				'taxonomy'   => 'product_cat',
    				'field_type' => 'select_advanced',
    			],
    		),
    	);
    
    	$meta_boxes[] = array(
    		'title'      => __( 'Box 2', 'lang' ),
    		'taxonomies' => 'product_cat',
    		'fields'     => array(
    			[
    				'type'       => 'taxonomy',
    				'name'       => esc_html__( 'Category', 'lang' ),
    				'id'         => 'field_2',
    				'taxonomy'   => 'product_cat',
    				'field_type' => 'select_advanced',
    			],
    		),
    	);
    
    	return $meta_boxes;
    } );

    If I select a category only in box 1 and save, the empty value is saved.
    If, however, I select a category in box 2 and save, both selects show the value I entered in box 2 even if the 2 fields have different IDs.
    So it always seems to save the value of the last taxonomy field.

    The problem only occurs if the 2 taxonomy fields have the same configuration for the taxonomy attribute ('taxonomy' => 'product_cat').

    Woocommerce version 8.0.3
    WP version 6.3.1
    Metabox version 5.7.5
    Meta Box AIO version 1.23.0

    #43183
    PeterPeter
    Moderator

    Hello,

    The taxonomy field sets the post-term so if you have two fields on a page, one will not work properly and it is expected. You should use one taxonomy field only.

    #43184
    Alessandro LioceAlessandro Lioce
    Participant

    Hello Peter,
    why "it is expected"?
    In my case I need to have 2 taxonomic fields for 2 different purposes in WooCommerce "product_cat" taxonomy.
    Why shouldn't it be possible to have 2 taxonomy fields with same configuration for the taxonomy attribute ('taxonomy' => 'product_cat')?
    Why can I use 2 text fields, but not 2 taxonomy!?
    Can you fix it?

    Screenshot admin Woocommerce product category page

    Thank you
    Alessandro

    #43185
    Alessandro LioceAlessandro Lioce
    Participant
    #43208
    PeterPeter
    Moderator

    Hello,

    As I said above, the taxonomy field sets the post terms. The last taxonomy field will override other taxonomy field values and all taxonomy fields will display the value of the last field.

    If you want to use it as 2 text fields which is the post meta (not post term), please use the field taxonomy_advanced.

    Read more in the documentation https://docs.metabox.io/fields/taxonomy/
    https://docs.metabox.io/fields/taxonomy-advanced/

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.