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


update_field doesn't update until I resave post

  • I’m using AJAX to post a form from the front end and creating a custom post type. The post is created. The fields a also posting and when editing the post in the admin section I see values in each field.

    foreach($dataArray as $key => $value) {
    update_field( $key, $value, $post_id );

    On the post template I am outputting the fields with

    echo "<pre>";
    $fields = get_fields();
    var_dump( $fields ); 

    I do not see any fields unless I update the post in the backend.

    If I see values in the fields in the backend why are they not appearing?

  • I’ve realized I need to use the field_key to update the fields since the post is being created.

    Is there a function which would allow me to get the field objects by name, so I can loop over fields by name and get the keys to update each field which contains a value from the form, rather than many update_field functions?

  • 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 );
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘update_field doesn't update until I resave post’ is closed to new replies.