Support

Account

Home Forums General Issues update_field doesn't update until I resave post Reply To: update_field doesn't update until I resave post

  • Not sure if this helps, but I’m doing something similar, and I just created a quick multi-dimensional array which maps the field keys and the values from my form on the front-end to the proper variables, then I can use a loop. I’m using Gravity Forms, but here’s some (brief) sample code:

    //Map the Custom Field Keys
    $selections['furnace']['key'] = 'field_5181b4065293c';
    $selections['ac']['key'] = 'field_5181c655cb339';
    $selections['filter']['key'] = 'field_5181c6e3cb33d';
    	
    //Map the values to the proper entry vars
    $selections['furnace']['value'] = $entry['1'];
    $selections['ac']['value'] = $entry['2'];
    $selections['filter']['value'] = $entry['3'];
    		
    //See if there is any previously saved data, and save the new values
    foreach ($selections as $snippet=>$item) {
    			
    	if (strlen($item['value'] > 1)) { //Don't need to do anything unless we have a value to save
    			
    		//We don't care in this case if there was a previous value; we always replace it with any passed data
    		update_field( $item['key'], $item['value'], $post_id );
    	}
    }