Home › Forums › Bug Reports › ACF Repeater sub-fields are losing / swapping content when re-ordered › Reply To: ACF Repeater sub-fields are losing / swapping content when re-ordered
This has not been completely tested and may cause timeout issue if used with options pages or excessive numbers of fields. There may be other conditions where it causes problems that I’m not aware of.
step 1, create this function
// this function with the help of
// @gabriel dot sobrinho at gmail dot com
// http://php.net/manual/en/function.array-merge-recursive.php
function acf_array_merge_recursive_distinct(array &$array1, array &$array2) {
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = $this->array_merge_recursive_distinct($merged[$key], $value);
} else {
// change from original code
// do not overwrite value in first array if it exists
if (!isset($merged[$key])) {
$merged[$key] = $value;
}
}
}
return $merged;
}
Now we’re going to create an acf/save_post filter
// low priority ensures it runs before anything else
// make lower if needed
add_filter('acf/save_post', 'acf_reorder_hidden_fields', -1);
function acf_reorder_hidden_fields($post_id) {
if (!isset($_POST['acf'])) {
// no need to run
return;
}
/*
You probably want to do some more checking here
for example only running on certain post types
or not running on options pages, etc, etc...
*/
// get existing field values for all fields of this $post_id
// we need to do this by getting the field objects
// and load unformulated values
// because we need to have field keys
$fields = get_field_objects($post_id, false);
// set up array to hold old values
$old_values = array();
foreach ($fields as $field) {
$old_values[$field['key']] = $field['value'];
}
// now using the custom array maerg function
$_POST['acf'] = acf_array_merge_recursive_distinct($_POST['acf'], $old_values);
}
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.