ACF Repeater fields – filter out empty rows from ImportWP

Recently I had the need a problem when importing data via Import WP into ACF Repeater fields would sometimes leave me with blank rows (ie. where there were blank entries in the imported data), or if I was editing a row, forgetting to remove any blank entries would just leave them in the system.

This is all fine if that’s what you wanted, but for my purposes, I needed to remove them.

The following is a piece of code (which you can include in your WordPress functions.php file (or custom plugin)) which gets run anytime a certain field (with the key “field_111222”) is updated (before being saved to the database).

It checks if certain sub-fields have been populated (line 3), and if neither of them have been populated, removes the row (line 4).

To use it, just update the field that the filter “watches” (last line) with the field you have, and update the field keys on line 3 to correspond to your own.

function my_pre_save($result, $group_id, $id) {
if($group_id === 'field_11223349e21') {
$result = array_filter($result, function ($row) {
return $row['field_1122334649e22'] || $row['field_66359a0549e23'] || $row['field_1122331049e24'];
});
}

return $result;
}
add_filter('iwp/acf/repeater/pre_save_value', 'my_pre_save', 10, 3);

Leave a Reply