Support » Plugin: CMB2 » Meta data is being deleted.

  • Resolved Bart Dabek

    (@raptor235)


    Hey guys…

    I have a set of fields setup which get either drawn or not based on which taxonomy the object is associated with. Everything works great, however if the user switches the taxonomy and then switches back again to the same taxonomy all the meta values are deleted.

    Here is an illustration:

    Original Content
    http://www.flowpress.com/screenshots/Screenshot_2017-04-11_10.43.16.png
    http://www.flowpress.com/screenshots/Screenshot_2017-04-11_10.43.29.png

    I’ll switch the catalog taxonomy and save
    http://www.flowpress.com/screenshots/Screenshot_2017-04-11_10.44.10.png
    Data is still in the db (which is what I want)
    http://www.flowpress.com/screenshots/Screenshot_2017-04-11_10.44.23.png

    Then I switch back to the original taxonomy and save
    http://www.flowpress.com/screenshots/Screenshot_2017-04-11_10.44.53.png
    http://www.flowpress.com/screenshots/Screenshot_2017-04-11_10.45.03.png

    All the original fields are zeroed out and deleted.

    As you can see below I’m not doing anything too complex just drawing the fields based on which taxonomy is selected and would like the data to remain if the user switches between taxonomies.

    
    function asd($a,$b){
    	global $product_fields;
    
    	$field_name = $a->args['name'];
    	$field_id   = $a->args['id'];
    	$catalog    = wp_get_post_terms( $a->object_id, 'catalog' );
    
    	// var_dump($product_fields[$catalog[0]->name]);
    	// var_dump($field_name);
    	// var_dump($field_id);
    	// var_dump($catalog[0]->name);
    	// var_dump($product_fields[$catalog[0]->name]);
    	// var_dump($product_fields[$catalog[0]->name][$field_name]);
    
    	if (isset($product_fields[$catalog[0]->name][$field_name])) {
    		return true;
    	}
    
    	if (isset($product_fields[$catalog[0]->name][$field_id])) {
    		return true;
    	}
    
    	return false;
    }
    
    function product(){
    
    	global $wpdb, $product_fields, $post;
    
    	$cmb = new_cmb2_box( array(
    		'id'            => 'product_data',
    		'title'         => __( 'Product Data', 'cmb2' ),
    		'object_types'  => array( 'product', ), // Post type
    		'context'       => 'normal',
    		// 'show_on'      => array( 'key' => 'id', 'value' => array( 2190 ) ),
    		'priority'      => 'high',
    		'show_names'    => true, // Show field names on the left
    		// 'cmb_styles' => false, // false to disable the CMB stylesheet
    		// 'closed'     => true, // Keep the metabox closed by default
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Watt Density', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'watt_density',
    		'type'       => 'text',
    		'attributes' => array(
    			'type' => 'number',
    			'pattern' => '\d*',
    		),
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Weight (kg)', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'weight-kg',
    		'type'       => 'text',
    		'attributes' => array(
    			'type' => 'number',
    			'pattern' => '\d*',
    		),
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Screw Plug Size', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'screw-plug-size',
    		'type'       => 'text',
    		'attributes' => array(
    			'type' => 'number',
    			'pattern' => '\d*',
    		),
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Item #', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'item-number',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Number Of Elements', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'number-of-elements',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Flange Type', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'flange-type',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Flange Size (In)', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'flange-size-in',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Immersed Length B (In)', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'immersed-length-b-in',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Immersed Length B (mm)', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'immersed-length-b-mm',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Power (kw)', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'power-kw',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Voltage', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'voltage',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Watt Density (W/cm²)', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'watt-density-wcmsup2',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Watt Density (W/incm²)', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'watt-density-winsup2',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Element Type', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'element-type',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Net Weight', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'net-weight',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Thermostat', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'thermostat',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    	$cmb->add_field( array(
    		'name'       => __( 'Terminal Enclosure', 'cmb2' ),
    		'desc'       => __( '', 'cmb2' ),
    		'id'         => 'terminal-enclosure',
    		'type'       => 'text',
    		'show_on_cb' => 'Wattco\assets\php\asd'
    		)
    	);
    
    }
    • This topic was modified 2 years, 11 months ago by Bart Dabek.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Me earlier: “That said, I am curious about the use of wp_get_post_terms() which is for taxonomy terms, while the rest of the code looks like it’d be saving to post meta. Perhaps part of what would be running issue here?”

    Bart’s reply: “That call is specifically looking at what the taxonomy is set for that product when it gets loaded. So it wouldn’t have anything to do with disappearance of meta data.”

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    One thing needing kept in mind is that when empty values come back a new submit for CMB2 fields, and there was a previous value saved, those values do end up getting removed, not preserved

    I did some quick cross reference, between the 3 stages shown for the database table, and this is what I found. The name of the field are all that are listed for the first image. The subsequent “Y” and “N” indicate if it persisted between saves. The only two that didn’t “survive” at all are the Screw Plug Size and the flange.

    watt density winsup2 Y N
    watt density wcmsup2 Y N
    voltage Y Y
    thermostat Y N
    terminal-enclosure Y N
    screw plug size N N
    power-kw Y N
    old_language Y Y
    old_item_id Y Y
    number of elements Y N
    net-weight Y N
    item number Y Y
    immersed length b-mm Y N
    immersed length b-in Y N
    image1 Y Y
    image Y Y
    flange N N
    element type Y N
    

    The reason I brought up wp_get_post_terms() was because of the thought of it possible ending up referencing empty values somehow, causing the fields to thus be lost upon save.

    Hi Michael,

    Is there anyway to prevent the system through a flag or filter from deleting those entries.

    If you think about it that data should stay in tact… the only difference between saving that same post_type is number of registered CMB2 fields… as far as CMB2 any other data in post_metadata should be left alone as it could be set by another plugin.

    If we think through the process in more detail.

    • post_type edit page gets shown with 10 fields
    • post_type saved with all the data
    • post_type taxonomy updated, causing on next refresh for different fields to be shown
    • post_type saved with new fields
    • you would expect all those field vales to stick around, no matter when the fields were registered, switching number of fields between refreshes shouldn’t trigger deletion of those fields
    • This reply was modified 2 years, 11 months ago by Bart Dabek.
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    I can understand both ways this could be handled. That said, this specific implementation here does feel a bit “off the beaten path”, which isn’t bad, just has important considerations.

    I think https://github.com/CMB2/CMB2/wiki/Box-Properties#hookup would be possibly the best route to go, even if some extra work. Both the “hookup” and particularly the save_fields right below it. Setting these to false would prevent CMB2 from doing things automatically for you, including removing of meta based on empty keys. The extra work mentioned here is that it’s up to you to save/update/remove data as necessary or desired.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Meta data is being deleted.’ is closed to new replies.