The ACF repeater field duplication problem refers to the issue where duplicating a repeater field in Advanced Custom Fields (ACF) results in the cloned fields not functioning properly. This can occur when using the ACF clone field functionality or manually duplicating the repeater field.
To solve this problem, you can use the acf_duplicate
filter provided by ACF. This filter allows you to modify the duplicated field data before it is saved. By using this filter, you can ensure that the duplicated repeater field works correctly.
Here’s an example code snippet that demonstrates how to use the acf_duplicate
filter to fix the ACF repeater field duplication problem:
function wpsnippets_fix_acf_repeater_duplicate( $duplicate, $post_id, $new_post_id, $field ) {
if ( $field['type'] === 'repeater' ) {
// Get the original repeater field value
$original_value = get_field( $field['name'], $post_id );
// Duplicate the repeater field value
$duplicated_value = array();
foreach ( $original_value as $row ) {
$duplicated_row = array();
foreach ( $row as $key => $value ) {
// Duplicate each sub-field value
$duplicated_row[ $key ] = $value;
}
$duplicated_value[] = $duplicated_row;
}
// Update the duplicated repeater field value
update_field( $field['name'], $duplicated_value, $new_post_id );
}
return $duplicate;
}
add_filter( 'acf_duplicate', 'wpsnippets_fix_acf_repeater_duplicate', 10, 4 );
In this code snippet, we define a custom function wpsnippets_fix_acf_repeater_duplicate
that is hooked into the acf_duplicate
filter. This function takes four parameters: $duplicate
(the default duplicated field data), $post_id
(the original post ID), $new_post_id
(the new post ID), and $field
(the ACF field object).
Inside the function, we check if the field type is a repeater. If it is, we retrieve the original repeater field value using get_field()
. Then, we iterate over each row and duplicate the sub-field values. Finally, we update the duplicated repeater field value using update_field()
.
By using this code snippet, the duplicated repeater field will have the correct values and work as expected.
Examples
Example #1: Duplicate ACF Repeater Field Rows
This example demonstrates how to duplicate rows within an ACF repeater field using the wpsnippets_duplicate_repeater_rows()
function.
<?php
function wpsnippets_duplicate_repeater_rows($repeater_field_name, $post_id) {
$repeater_field = get_field($repeater_field_name, $post_id);
$new_rows = array();
if ($repeater_field) {
foreach ($repeater_field as $row) {
$new_rows[] = $row;
}
}
update_field($repeater_field_name, $new_rows, $post_id);
}
This code retrieves the existing rows from an ACF repeater field, creates a new array to store the duplicated rows, and then updates the repeater field with the new rows.
Example #2: Duplicate Specific ACF Repeater Field Rows
This example demonstrates how to duplicate specific rows within an ACF repeater field using the wpsnippets_duplicate_specific_repeater_rows()
function.
<?php
function wpsnippets_duplicate_specific_repeater_rows($repeater_field_name, $post_id, $row_indexes) {
$repeater_field = get_field($repeater_field_name, $post_id);
$new_rows = array();
if ($repeater_field) {
foreach ($repeater_field as $index => $row) {
if (in_array($index, $row_indexes)) {
$new_rows[] = $row;
}
}
}
update_field($repeater_field_name, $new_rows, $post_id);
}
This code retrieves the existing rows from an ACF repeater field, checks if the row index is included in the specified array of row indexes to duplicate, and then updates the repeater field with the duplicated rows.
Example #3: Duplicate ACF Repeater Field Rows with Modified Values
This example demonstrates how to duplicate rows within an ACF repeater field while modifying specific field values using the wpsnippets_duplicate_repeater_rows_with_modifications()
function.
<?php
function wpsnippets_duplicate_repeater_rows_with_modifications($repeater_field_name, $post_id, $modifications) {
$repeater_field = get_field($repeater_field_name, $post_id);
$new_rows = array();
if ($repeater_field) {
foreach ($repeater_field as $row) {
$new_row = $row;
foreach ($modifications as $field_name => $new_value) {
$new_row[$field_name] = $new_value;
}
$new_rows[] = $new_row;
}
}
update_field($repeater_field_name, $new_rows, $post_id);
}
This code retrieves the existing rows from an ACF repeater field, creates a new array to store the duplicated rows with modified values, and then updates the repeater field with the new rows. The modifications are specified as an associative array where the keys represent the field names to modify and the values represent the new values for those fields.