Support

Account

Home Forums General Issues update_field not updating fiel

Solved

update_field not updating fiel

  • Please see my answer here in another topic where there was an issue updating the sub field of a group field https://support.advancedcustomfields.com/forums/topic/field-value-update-using-update_field-doesnt-show-on-backend/#post-162342

  • Hi ACF, I am struggling to understand how to get this working. If someone can please help, much appreciated. Thank you.

  • How is it that field_64c782fc4c55d is a sub field and this doesn’t work:

    echo get_sub_field(field_64c782fc4c55d, 1074);

    but this works

    echo get_field(field_64c782fc4c55d, 1074);

  • Above also explains why I get this error:

    update_sub_field(array(‘field_64b38c4e3ec56’, ‘field_64c782fc4c55d’),11, 1074);
    not a sub_field <– add that to update_sub_field() function.

  • Your posts are full of conflicting information.

    You are trying to update a field with the key field_64c782fc4c55d and that it is not a sub field.

    Later you say that the ID of the field is acf-field_64b38c4e3ec56-field_64c782fc4c55d

    This clearly shows that this field is a sub field of some kind because that is the only reason that the ID would contain 2 field keys.

    At this point I really have not idea what your field are or what you are working with, nor can I tell why the update is not working.

    Please provide a PHP export of the field group containing this field.

  • No, I have stated previously that field_64c782fc4c55d is in the group field_64b38c4e3ec56. For field_64c782fc4c55d it says at the top of the group, “sub fields” and field_64c782fc4c55d is under that.

    When I stated acf-field_64b38c4e3ec56-field_64c782fc4c55d, it is from the view html. Here it is again.

    <input type="number" id="acf-field_64b38c4e3ec56-field_64c782fc4c55d" name="acf[field_64b38c4e3ec56][field_64c782fc4c55d]" step="any">

  • Did not know I could export php code. I am looking into where that is now.

  • This reply has been marked as private.
  • I found the export. I don’t wish to share with everyone the php. Can I send it privately somehow please?.

  • I find this board buggy too. I have submitted more info and it gives an error saying I said this before, however, it isn’t displayed in the thread.

  • Error: Duplicate reply detected; it looks as though you’ve already said that.
    It is so very bizarre and do not understand what is going on. I am hoping you can answer some of my previous questions. It would help me understand what’s going on.

    As already explained under FIELD GROUPS I have:

    Name: FRESH SNOW SKI RESORTS”
    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 and here is an example of one:

    Field Name: qgis_aa_zoom
    Key: field_64c782fc4c55d
    Numeric

    Now, if I do this;

                $result = update_field(field_64c782fc4c55d,11, 1110);
                $arraydata = get_field_object(field_64c782fc4c55d,1110);
                echo $arraydata['value'];

    I get 11. If I change to 7, I get 7. It seems to update. Is this because I am creating a new entry as a parent? Why would this work? When I look at my sub field view, it is not updated.

    Now, if I do this:

                $result = update_sub_field(array('field_64b38c4e3ec56', 'field_64c782fc4c55d'), 3, 1110);
                $arraydata = get_field_object(field_64c782fc4c55d,1110);
                echo $arraydata['value'];

    It is not updated and gives me the old value from above on the screen.

  • You can set the reply as private, only you and site users that can view private replies (like me) will see it.

    If it is a sub field then you cannot use update_field() to update it. This function is only for top level fields. To update sub field you must use update_sub_field() or use some other way to update the entire field and all sub fields at the same time, as I outlined it a this solutions that I referred you to in a previous reply https://support.advancedcustomfields.com/forums/topic/field-value-update-using-update_field-doesnt-show-on-backend/#post-162342

    It is not that confusing, if it is a sub field then you are using the wrong function/method to update the value.

  • You stated, “It is not that confusing, if it is a sub field then you are using the wrong function/method to update the value.” but it is. See below:

    Name: FRESH SNOW SKI RESORTS”
    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 and here is an example of one:

    Field Name: qgis_aa_zoom
    Key: field_64c782fc4c55d
    Numeric

    I assume field_64c782fc4c55d (qgis_aa_zoom) is a sub field, right?

    Why does this work (although it does not show in the view as mentioned previously):

    $result = update_field(field_64c782fc4c55d,11, 1110);
    $arraydata = get_field_object(field_64c782fc4c55d,1110);
    echo $arraydata['value'];

    and this not work;

    $result = update_sub_field(array('field_64b38c4e3ec56', 'field_64c782fc4c55d'), 3, 1110);
    $arraydata = get_field_object(field_64c782fc4c55d,1110);
    echo $arraydata['value'];

    Is there any other way to post my php with out giving it to logged in users?

  • Hello All. Thanks for the support thus far. If anyone has ideas above above, really appreciated. It now 2 weeks and unable to move forward 🙁 Thank you.

  • Did you read the solution I gave in the other topic?
    Here it is adapted for your field keys

    
    // initialize row value just in case the loop returns noting
    $row = array();
    // loop group field and get existing values
    if (have_rows('field_64b38c4e3ec56', $post_id)) {
      // this is always true once
      while (have_rows('field_64b38c4e3ec56', $post_id)) {
        // this loop always happens 1 time for a group field
        // *********************************************************
        // little known fact
        // the_row() returns an array holding the row
        // with field keys as indexes and unformatted field values
        // *********************************************************
        $row = the_row();
      }
    }
    // set a new value for the "sub field" that needs to be updated.
    $row['field_64c782fc4c55d'] = 'value you want to update to here';
    // update the group field
    update_field('field_64b38c4e3ec56', $row, $post_id);
    
  • Yes, that works. Thank you. However, I cannot understand why this doesn’t work:

    $result = update_sub_field(array('field_64b38c4e3ec56', 'field_64c782fc4c55d'), 3, 1110);

  • There seems to be an issue with updating sub fields of a group field. It is treated as a repeater, as was discussed in the other topic.

    A group field is actually a repeater that always has one row. update_sub_field() is looking for a row index in the second array index array($repeater_key, $row_index, $sub_field_key). There is no row index for a group field and if you supply a row index ACF will save the value using the incorrect meta key of "{$repeater_name}_{$index}_{$sub_field_name}"

    In the other thread the person was updating the sub field inside a have_rows() loop and ACF was assuming the index of 0, which would be correct for the first row of a repeater.

    There is likely a bug, I can’t fix bugs, this would have to be reported to the developers. I don’t use update_sub_field() as a general rule so it is only recently, that other topic, that brought it to my attention. My opinion when updating repeaters, or group fields, is to use a similar method to this, updating the entire repeater rather than a sub field. On the other hand, I almost never update fields from code.

  • Thank you very much for the detailed explanation and it makes a lot of sense now.

    When is a subfield not part of a group though?

    I see in the forumn there is one for bugs. Do I add a bug there?

    Thanks again!

  • I thing you are still confusing “Field Groups” and “Group fields”

    All fields are in “Field Groups”. A “Group field” is a specific type of field that has sub fields used to hold multiple fields in logical sections.

    As far as bugs, the developers don’t visit or watch this forum. The only people here are other users. You would have to contact them directly with this form or by adding a ticket in your account.

  • I got it workin outside of a loop, finally, with this;

    update_field('field_64b38c4e3ec56', array(field_64c782fc4c55d=>7), 1123);

    so it is:

    update_field($group_field_name, array($sub_field_name=>$new_value), $post_id)

    I found it from this thread. Search for this sentence, “The true way for update / create a sub field of a group is with this ” by Sebastien Patelski dated October 28, 2021.

    I think this deserves some highlighting in the man pages.

    Again, that you for all your help John Hueber.

  • I am fighting with update_field as well and have been for some days. I’ll reference this thread when filing a bug report in a minute.

Viewing 22 posts - 26 through 47 (of 47 total)

You must be logged in to reply to this topic.