I am trying to move a Repeater field from a parent page into one created with wp_insert_post() and am running into a bit of trouble.
$new_post_id = wp_insert_post($new_post);
$field_key = "field_53e4ed1e04b91";
$value = get_field('downloads',$id);
update_field( $field_key, $value, $new_post_id );
I have found the field_key in my _postmeta table and have tried using that but the problem is that when i run wp_insert_post(), the data on the new post seems to be serialized whereas data on other posts seems to be in the following format
field name in ACF is ‘downloads’
_postmeta table
meta_key meta_value
downloads_0_file 10966
_downloads_0_file field_53e4ed1e04b91
downloads_1_file 10962
_downloads_1_file field_53e4ed1e04b91
downloads 2
_downloads field_53e4ed1604b90
I am using ACF Pro but were previously using ACF
When i run the script at the top, the meta_key just returns as ‘file’ with meta_value as serialized data
Not sure what i’m doing wrong, any ideas would be much appreciated
The only think that I can think of is that get_field()
is returning an array and the sub fields returned are by field name and you should be using the field keys for each sub field when updating.
You don’t need to inspect _postmeta table to get the field keys. They are available when editing a field group under “screen options”
What you’ll need to do is convert all the names to field keys
$new_post_id = wp_insert_post($new_post);
$field_key = "field_53e4ed1e04b91";
$sub_field_list = array(
'field_name_1' => 'field_key_1',
'field_name_2' => 'field_key_2',
// etc...
)
$value = get_field('downloads',$id);
$new_value = array();
if (count($value)) {
foreach ($value as $row=> $sub_fields) {
$new_value[$row] = array();
foreach ($sub_fields as $sub_field_name => $sub_field_value) {
$new_value[$row][$sub_field_list[$sub_field_name]] = $sub_field_value;
}
}
}
update_field( $field_key, $new_value, $new_post_id );
Worked like an absolute charm, you my sir are a legend, thanks.