Home Forums General Issues update_field not updating fiel


update_field not updating fiel

  • I am updating a custom post type using ACF’s function, update_field(). According to the documentation, I must use the field key instead the field name which I am doing. Here is my code;

                   if (update_field($key, $aa_winter_sports_point->properties->$value, $existing_aa_winter_sports_point->ID)){
                        // new data or $succes = 'success | ';
                        echo 'SUCCESS - update_field('.$key.','.$aa_winter_sports_point->properties->$value.','.$existing_aa_winter_sports_point->ID.')</br>';;
                    } else {
                        echo 'FAIL or NO UPDATE - update_field('.$key.','.$aa_winter_sports_point->properties->$value.','.$existing_aa_winter_sports_point->ID.')</br>';

    Here is my output:

    update_field($key=field_64c782fc4c55d, $value=84, $id=1074)
    FAIL or NO UPDATE - update_field(field_64c782fc4c55d,84,1074)

    I have checked and hte record 1074 exists. The field is a number. I also tried to insert as a string, “84” but also did not work. I tried the WP function aswell and it did not work:

    update_post_meta($existing_aa_winter_sports_point->ID, $key, $aa_winter_sports_point->properties->$value )
  • btw, all the fields are part of a group. Does that make a difference? I know using field names the field group must be pre-concantonated with the field name. I assume using the key, this is not required as the key is guaranteed to be unique.

  • update_field() returns the meta ID if the meta key (field name) did not exist, it returns true if the value is altered, it returns false if the call fails or the value is not altered.

    This is the same as the return all WP meta update functions. ACF simply returns what is returned by these functions.

  • Yes, I get that, however, the fields are not updated. There is not much debugging info other that it do not succeed. I echod out the update_field values and all seems correct.

    Any ideas?

  • The data I am trying to update are sub fields in a group.

    I see there is the following function;

    update_sub_field($selector, $value, [$post_id]);

    I have tried below but it also doesnt work.

    update_sub_field(field_64c782fc4c55d, 84, 1074);

  • @John Huebner
    hoping you may have some ideas for me to try. Thank you as always!

  • When using update sub field in this manner you must supply an array for the ancestors and sub field for the selector

    array('group field key', 'sub field key');
  • btw, all the fields are part of a group. The those fields considered sub fields? There are no repeater fields.

  • You said sub field in a group and I assumed you meant a group field.

    There can be confusion sometimes.

    A Field Group is what you edit in ACF. Fields at the top level of the group are not considered sub fields.

    A Group field is a specific type of field that has sub fields. Repeaters and Flexible content fields also have sub fields.

  • Thank you and then I understand the fields are am trying to update are not sub fields. Wonder if there is any other debugging I can do to help understand why my fields are not getting updated?

  • This is getting weirder and weirder and beginning to think there is a bug somewhere.

    Here are two field I have:

    Key: field_64c782fc4c55d
    Name: qgis_aa_zoom
    Group: fs_ski_resort_data

    Key: field_64a54023795f5
    Name: fs_aa_zoom
    Group: fs_ski_resort_data

    I have tried the most basic code as follows to update;

    update_field('field_64c782fc4c55d', 12, 1074);

    If I go to my custom posts and look at the field, it is empty, however, if I do this;

    echo 'field_64c782fc4c55d: '.get_field(field_64c782fc4c55d, 1074);

    I get 12, as updated above. Bizzare.

    I have an API that calls the field by name, rather than key as follows;

    `$fs_ski_resorts_data[$i][‘fs_ski_resort_aa_zoom’] =
    get_field(‘fs_ski_resort_data_fs_aa_zoom’) ?:
    get_field(‘fs_ski_resort_data_qgis_aa_zoom’); `

    Above produces: "fs_ski_resort_aa_zoom": "", // empty string

    If I manually insert data via the view of my custom post type, say, 7, I get this;

    "fs_ski_resort_aa_zoom": "7",

    So there seems to be something going on in the database and things are not matching correctly.

    Any help appreciated.

  • The only other thing that could be causing it, that I know of, would have to do with when in the load/initialization of WP the update occurs. If the update is happening before ACF is initialized then this could have unexpected results. This is also the case with any calls to ACF functions.

  • When I call update_field(), I call it using ajax as follows

    add_action('wp_ajax_get_myFunction', 'myFunction');

    I would have thought that if update_field() is indeed firing, the ACF libraries would have loaded, no?

  • Yes, everything in initialized before WP does AJAX actions, so that eliminates a timing issue during save.

    I honestly don’t see any reason there is an issue.

    Going back to your code

    update_field($key, $aa_winter_sports_point->properties->$value, $existing_aa_winter_sports_point->ID)

    is it possible that $aa_winter_sports_point->properties->$value or $existing_aa_winter_sports_point->ID has an incorrect value.

  • Per the first post, I have echoed out the values being applied to the update_field() function and they are correct.

    I have tried this, too:

                if (update_field('field_64c782fc4c55d', 2, '1074')){
                    echo "Success - ";
                    echo 'field_64c782fc4c55d: '.get_field(field_64c782fc4c55d, 1074);
                    echo '</br>';

    As expected, it will not update is I keep 2 in there more than once. If I change 2 to another number, update_field is executed and the value of field_64c782fc4c55d is updated and get_field returns the updated value.

    I then go to the view of my custom post type and the value is never update. For instance, if I update the via the view of field field_64c782fc4c55d and I enter 7, the number never changes despite executing the above code.

    So then it means the view is not right. I inspected the html and it shows;

    <div class="acf-input-wrap"><input type="number" id="acf-field_64b38c4e3ec56-field_64c782fc4c55d" name="acf[field_64b38c4e3ec56][field_64c782fc4c55d]" value="7" step="any"/></div></div>

    The id is acf-field_64b38c4e3ec56-field_64c782fc4c55d which is myFieldGroupName-FieldKey

    Is that the way it should be working? There is not a description in the man pages and I would hate to start digging through the ACF code.

  • this
    indicates to me that this is a sub field that you are trying to update. What type of field is the parent field? field key field_64b38c4e3ec56

  • under FIELD GROUPS I have:

    Key: group_647d9f906d00a

    In the above group I have FIELDS:

    Name: FS SKI RESORT DATA (and it is defined as a group
    Key: field_64b38c4e3ec56

    In above, I have many sub fields but the one that is not working is;

    Field Name: qgis_aa_zoom
    Key: field_64c782fc4c55d

  • Yes, the field you are attempting to update is a sub field, which I’ve gone over already.

    You can’t use update_field() to update a sub field of a group field. At a minimum you should be using update_sub_field(). This function requires passing an array of the field hierarchy as the selector/field_name

    update_sub_field(array('field_64b38c4e3ec56', 'field_64c782fc4c55d'), $value, $post_id); 
  • Per far above, I have actually tried:

    update_sub_field(field_64c782fc4c55d, 84, 1074);

    And it does not update even if the value is different from the current value.

    According to the man page, $selector can be a string, too,

    $selector (string|array) (Required) The sub field name or key, or an array of ancestors and row numbers.
    $value (mixed) (Required) The new value.
    $post_id (mixed) (Optional) The post ID where the value is saved. Defaults to the current post.

  • Even this board is buggy! Wrote an entirely different reply to above and my oly reply is posted. Rewriting now.

  • Per far above, I have already tried;

    update_sub_field(field_64c782fc4c55d, 84, 1074);

    and it does not work even if the value changes.

    I found update_sub_field(); in /plugins/advanced-custom-fields-pro/includes/api/api-templated. I added some debugging code to it, too.

    function update_sub_field( $selector, $value, $post_id = false ) {
    	// vars
    	$sub_field = false;
    	// get sub field
    	if ( is_array( $selector ) ) {
    		$post_id   = acf_get_valid_post_id( $post_id );
    		$sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
    	} else {
    		echo 'The $selector is '.$selector.', the $value is '.$value.' and the $post_id is '.$post_id;
    		echo ' Also, this is not an array</br>';
    		$post_id   = acf_get_loop( 'active', 'post_id' );
    		echo 'post_id: '.$post_id.'</br>';
    		$sub_field = get_row_sub_field( $selector );
    		echo 'sub_field: '.$sub_field.'</br>';
    		echo $post_id;
    	// bail early if no sub field
    	if ( ! $sub_field ) {
    		echo "not a sub_field</br>";
    		return false;
    	// update
    	echo "hello".$value.'//'.$post_id.'//'.$sub_field.'</br>';
    	return acf_update_value( $value, $post_id, $sub_field );

    The output of the above is;

    The $selector is field_64c782fc4c55d, the $value is 84 and the $post_id is 1074 Also, this is not an array
    not a sub_field

    post_id is not returned, sub_field is not return and it bails early as it is not a sub_field.

  • Sorry, I see that if I use update_sub_field() outside of have_rows(), I must specify the row and parent. You said this:

    update_sub_field(array('field_64b38c4e3ec56', 'field_64c782fc4c55d'), $value, $post_id);

    I have tried:

    $result = update_sub_field(array('field_64b38c4e3ec56', 'field_64c782fc4c55d'),5, 1074);
    echo $result;

    I get:

    not a sub_field

    I can see where I specify the parent, however, when do I speficy the row. I guess row would be used for a repeater field and assume 1 if not?


  • I have traced it down to function acf_maybe_get_sub_field(). This is part of the function:

    	if ( ! is_array( $selectors ) || count( $selectors ) < 3 ) {
    		return false;

    The number of $selectors is 2 so it fails. Should there be a three?

    If I print_r($selectors) I get:

    Array ( [0] => field_64b38c4e3ec56 [1] => field_64c782fc4c55d )

    …meaning there are only 2 selectors and I guess it is expecting a min of 3, right?

  • I have also tried:

    update_sub_field(array('field_64b38c4e3ec56', 1, 'field_64c782fc4c55d'),11, 1074);

    Cannot figure it out.

  • Any help here appreciated.

Viewing 25 posts - 1 through 25 (of 47 total)

You must be logged in to reply to this topic.