Last updated on September 14, 2023

ACF remove_row() not deleting repeater rows

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Deleting Rows in ACF Repeaters with remove_row().

The remove_row() function in Advanced Custom Fields (ACF) is used to delete a specific row within a repeater field. However, there are cases where the remove_row() function may not work as expected and fail to delete the repeater rows. In such situations, you can use the following code snippet to manually delete the rows using the ACF API.

function wpsnippets_delete_repeater_row($field_key, $row_key) {
    $field = get_field_object($field_key);
    $value = get_field($field_key);

    if (isset($value[$row_key])) {
        unset($value[$row_key]);
        update_field($field_key, $value);
    }
}

This code snippet defines a custom function wpsnippets_delete_repeater_row() that takes two parameters: $field_key and $row_key. The $field_key is the key of the repeater field, and the $row_key is the key of the row you want to delete.

To use this function, you need to pass the appropriate field key and row key. For example:

wpsnippets_delete_repeater_row('your_repeater_field', 2);

In the above example, the function wpsnippets_delete_repeater_row() will delete the row with the key 2 from the repeater field with the key your_repeater_field.

This code snippet can be useful when you encounter issues with the remove_row() function in ACF and need an alternative method to delete repeater rows.

Last updated on September 14, 2023. Originally posted on September 11, 2023.

Leave a Reply