ACF Group Field in add_post_meta?
Question
how I can fill field in ACF Group Field in add_post_meta?
This is maybe useful, I tried something, but it doesn’t work.
$values = array('value 1', 'value 2', 'value 3');
Now the code to insert the repeater values:
$repeater_field = 'repeater';
$repeater_key = 'field_5443d4e2dd4e4';
$sub_field = 'sub_field';
$sub_field_key = 'field_5443d4e2dd4e5';
$count = count($values);
if ($count) {
// the db value stored in the db for a repeater is
// the number if rows in the repeater
add_post_meta($post_id, $repeater_field, $count, true);
add_post_meta($post_id, '_'.$repeater_field, $repeater_key, true);
for ($i=0; $i<$count; $i++) {
// the actual field name in the DB is a concatenation of
// the repeater field name, the index of the current row
// and the sub field name, with underscores added
$sub_field_name = $repeater_field.'_'.$i.'_'.$sub_field;
add_post_meta($post_id, $sub_field_name, $values[$i], false);
add_post_meta($post_id, '_'.$sub_field_name, $sub_field_key, false);
}
}
But I need a group. In my case but for the example above, I don’t have array, just single value.
in progress
0
advanced-custom-fields, post-meta
4 years
2020-03-27T12:51:21-05:00
2020-03-27T12:51:21-05:00 1 Answer
128 views
0
Answer ( 1 )
$field_name = $repeater_field(1).‘_’.$sub_field(2);
add_post_meta($post_id, $sub_field_name, VALUE TO INSERT);
(1) The exact name of the parent field group.
(2) The exact name of the subfield inside the parent.
For example:
Name of the group in ACF: fruits
Name of subfield inside this group: banana
add_post_meta($post_id, fruits_banana, VALUE TO INSERT);