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_[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);
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/
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2022 Advanced Custom Fields.