/**
* Function: update_acf_repeater_field
* Parameters: $meta_type = post|user|comment
* $field_group_name = the name of ACF Field Group
* $repeater_field_name = the repeater field name
* $subfields = a multidimensional array with the array keys matching the
* the sub field names, e.g., array(array('subfield_name'=>'value','subfield_name'=>'value'))
* $post_id = the post id, user id or comment id
*
* Developer: Patrick Ingle (https://github.com/patrickingle)
*
* Problem Resolved: Adding an ACF repeater field to a new post, comment or user.
*
*/
function update_acf_repeater_field($meta_type,$field_group_name,$repeater_field_name,$subfields=array(),$post_id) {
global $wpdb;
foreach($subfields as $key => $subfield) {
foreach ($subfield as $subkey => $value) {
update_metadata($meta_type, $post_id, $repeater_field_name.'_'.$key.'_'.$subkey, $value);
update_metadata($meta_type, $post_id, '_'.$repeater_field_name.'_'.$key.'_'.$subkey, $repeater_field_name.'_'.$key.'_'.$subkey);
}
}
$sql = "SELECT pm.meta_key FROM ".$wpdb->prefix."postmeta pm JOIN ".$wpdb->prefix."posts po ON po.ID = pm.post_id WHERE pm.meta_value LIKE '%$repeater_field_name%' AND po.post_title="$field_group_name"";
$field_key = $wpdb->get_results($sql,ARRAY_A)[0]['meta_key'];
update_metadata($meta_type, $post_id, $repeater_field_name, count($subfields));
update_metadata($meta_type, $post_id, '_'.$repeater_field_name, $field_key);
}