The add_row()
function in Advanced Custom Fields (ACF) is used to add a new row to a repeater field. However, there are a few common reasons why add_row()
may not be adding rows to the repeater. Let’s explore some possible solutions.
One reason could be that the repeater field is not properly registered. Make sure you have registered the repeater field using the acf_add_local_field_group()
function. Here’s an example of how to register a repeater field:
function wpsnippets_register_fields() {
acf_add_local_field_group(array(
'key' => 'group_123456789',
'title' => 'My Repeater Field',
'fields' => array(
array(
'key' => 'field_123456789',
'label' => 'Repeater',
'name' => 'repeater_field',
'type' => 'repeater',
'sub_fields' => array(
// sub fields configuration goes here
),
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
));
}
add_action('acf/init', 'wpsnippets_register_fields');
Another reason could be that you are not specifying the correct parent field key when calling add_row()
. The parent field key is the key of the repeater field. Here’s an example of how to use add_row()
correctly:
$parent_field_key = 'field_123456789'; // Replace with your repeater field key
$new_row = array(
'field_name' => 'field_value', // Replace with your sub field name and value
);
add_row($parent_field_key, $new_row);
Make sure to replace 'field_123456789'
with the actual key of your repeater field, and 'field_name'
and 'field_value'
with the name and value of your sub field.
Lastly, ensure that you are calling add_row()
within the appropriate context, such as within a loop or a hook. If you are trying to add rows outside of the appropriate context, it may not work as expected.
By following these guidelines, you should be able to successfully add rows to a repeater field using the add_row()
function in ACF.