Support

Account

Home Forums Feedback Field name vs field key in update_post

Solved

Field name vs field key in update_post

  • Is there a difference between using the field key versus the field name in update_field?

    I’m using a form shortcode to capture post information and use it to set ACF fields with update_field. In most cases this works well using the field key. (I’m working though a course and the instructor used they key.)

    But I couldn’t get it to work with the subfields of a group field. When I switched to using [group-name]_[subfield-name], it worked. At first I thought I must have had the key wrong, but I tried both field_[subfield-key] and field_[group-key]_field_[subfield-key] and neither did the job. So what gives?

    If there is no difference between using the key or the name in this case, perhaps we need better documentation of group subfields. Does the parent group key get appended before the subfield key the same way the names are?

  • It is usually always better to use field keys vs field names for updating fields. The reason for this is for fields that do not yet exist because ACF will not know what type of field the field name refers to when the field does not exist yet.

    I am assuming that you are referring to a “group” field in [group-name]_[subfield-name] an not a “Field Group”. The group field is a special type of repeater field. When using update field you would do something like

    
    $values = array(
      // sub field key => value of each sub field
      'key_12345' => 'sub field 1 value',
      'key_23456' => 'sub field 2 value'
    );
    update_field($group_field_key, $values);
    
  • Thanks John, that worked for me. I would still suggest having that in the Group Field documentation, or making it more obvious, since I wasn’t able to find it.

  • Using the field key and why is explained in the update_field() documentation, which is where I think the developer expects people to look when they’re using that function. There are also examples of updating several types of fields, including a flex field, which is the same as a repeater with the addition of ‘acf_fc_layout’. A group field is also another form of repeater as example code for a loop shows. I know that figuring out what the value should be may be complicated and these things are not spelled out explicitly. With the number of field types and the different types of data they hold, most of these details are really left for us to figure out.

    I can’t really do anything about the documentation. I’m not the developer and I don’t maintain this site. I’m just here to help out on this forum. If you want to submit suggestions for documentation improvements you can probably open a new support ticket here: https://support.advancedcustomfields.com/new-ticket/

  • Thanks John. My mistake; I’ve seen you post on so many threads around here that I assumed you were involved in the development. I will submit a ticket with my comments as you suggested.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Field name vs field key in update_post’ is closed to new replies.