Home › Forums › General Issues › Updating fields in Groups in acf/save_post
I’m swithcing some of my custom post types over to use the generally very useful (for my end users) Groups Layout object (https://www.advancedcustomfields.com/resources/group/), but I’ve hit a bit of a snag.
I need to update some of the content on save, using the action hook acf/save_post. This worked fine before I shuffled a bunch of fields (that were at the root of the ACF object) into Groups. Now I can’t find an ACF method/variables for them that will allow me to update the fields within a group.
I even tried it by doing my checking/updating with a have_rows loop. While in the have_row loop for a group layout object, I can definitely use:
get_sub_field(‘field_name’);
just fine, but when I try:
update_sub_field(‘field_name’, $new_field_name_value);
It doesn’t do anything (and the value returned by update_sub_field is false);
I’ve tried it outside the loop as well, using the
update_sub_field(array(‘group_field_name’, 1, ‘field_name’), $new_field_name_value);
approach, but I also get a false return value/no updating happening.
Am I missing something, or is there just currently not a way to update an ACF group field using ACF method.
In the interim, I’m switching over to just using the WP core update_post_meta approach so I can ship this update out, but I’d prefer to use native ACF function to do it, since I know that bypassing ACF functions to update ACF values is, generally; not a good idea && frowned upon && asking for trouble later. 😉
Hi @rslater
Are you trying it this way:
Suppose a Group field named ‘hero’ with a sub field named ‘image’… it will be saved to the database using the meta name ‘hero_image’
Yeah, that’s what I ended up doing to get the project wrapped today.
I did a quick grab and dump of get_post_meta() to the error log from within my acf/save_post function, then watched it to see which of the many variables was the one acf was actually using. I realized I’d need to do that after I noticed moving fields into a new group for an existing custom post doesn’t actually clean up any of the old post_meta values– it just leaves the old post_metas there, creates new ones and starts using them. I think it may even have left/used the same ACF key_hashvalue in there, so now it appears twice in the post_meta (one of the reasons I was concerned about just using update_post_meta and similar WP functions instead of leveraging an ACF function to manage grouped fields).
By the way, in case anyone else runs into this, @keithlock is spot on. ACF, when creating page_metas for groups, does so in the format:
group_pretty_name . “_” . field_pretty_name
In case anyone else runs into this problem and wants to dump stuff to the log to watch what’s going on during your custom function hooked in to acf/save_post, I found this little snippet super helpful (wish I had recorded what site/who I grabbed it from, but in my rush to solve this problem I forgot to — needless to say, not my original code solution here 😉
error_log(“Meta values of post:”);
ob_start();
var_dump(get_post_meta($post_id));
error_log(ob_get_clean());
Since all this stuff is happening at a point you can’t just echo the returned value of a get_post_meta() var_dump to the screen, I had to look up that little gem to get the var_dump into the log.
The value does get dumped as one long string, but if you bring it into regular expression compliant application like notepad++, you can do a reg ex find/replace for \\n to \r\n and quickly have a human readable dump of the page_meta to scan and find what ACF is doing where.
Updating the post_meta value directly is currently working fine (so far as I can tell– I have my acf/save add_action priority set to the suggested level of 20… doing it at other priority levels might cause some oddness…). But I’d much prefer an ACF native way of doing this, which so far as I can tell, doesn’t currently exist. I’m a little concerned about the direct update_post_meta (and in another location, delete_post_meta) that I’m using currently to work around this, since a quick google search of acf update_post_meta|delete_post_meta turns up all kinds of threads where people (and some acf support folks/devs) seem to be waiving everyone off from doing that, in favor of acf methods (a literal, archaic/original use of deprecation here? 😉
The documentation for this seems to be incomplete. By trail and error I used the following solution:
//Group your data into an array:
$values = array(
'street' => $street,
'number' => $number,
'zipcode' => $zipcode,
'city' => $city
);
//Update the field using this array as value:
update_field( 'selector', $values, $post_ID );
PLEASE, write link to this topic, especially to second post in article about update_sub_field. It usefull for me, and maybe usefull for other people. I wasted about 3 hours to understand why my group fields not updated, while not saw this topic and after reading carefully second answer about underscore “_”.
Arghhh…
@keithlock I am facing same issue updating main group with another group as sub-field.
So it is like
maingroup_subgroup_subgroupfield
If I want to update fields of subgroup, I need to pass maingroup with an array of subgroup and its values,
When maingroup doesn’t exist, and updating fields of subgroup
update_field('maingroup', $values ,$post)
it returns true
But when maingroup exists, and updating fields of subgroup
update_field('maingroup', $values ,$post)
it returns false
Isn’t there still a native ACF Function to update single field of a group?
The true way for update / create a sub field of a group is with this :
update_field('the_group', array('the_element_to_update'=>$value), $post_id)
With this way :update_field('the_group_the_element_to_update', $value, $post_id)
If the field has’nt been created before, it don’t work, because in the database the meta_data _the_group_the_element_to_update is empty and then, you can’t get the value of the field.
`get_field(‘the_group’, $post_id)[‘the_element_to_update’] is empty.
The topic ‘Updating fields in Groups in acf/save_post’ is closed to new replies.
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.