Home › Forums › Add-ons › Repeater Field › delete_row() function leaves empty row
Hi there!
I have an ACF repeater field with three subfields (name, email, date). On a website what I’m working now users can edit their schedule which is stored into repeater. Users can add and delete any event in the schedule. When I’m trying to delete ‘not the last row’ from repeater, on a place of deleted row appears empty row.
For example, we have three rows which was created through admin panel. After deleting second one with ‘delete_row()’ function, we will get three rows, but second row is empty.
Here is my code:
$success = delete_row('field_56d4192e62d2c', $_POST['row_id'], $_POST['post_ID']);
I tried even with field name:
$success = delete_row('rides_list', $_POST['row_id'], $_POST['post_ID']);
$_POST[‘row_id’] = 2 and $_POST[‘post_ID’] = 623
Here is what I get in wp_postmeta:
| 6887 | 623 | rides_list | 3
| 7157 | 623 | rides_list_0_rider_name | Test
| 7158 | 623 | _rides_list_0_rider_name | field_56d419a662d2d
| 7159 | 623 | rides_list_0_rider_email | [email protected]
| 7160 | 623 | _rides_list_0_rider_email | field_56d419c762d2e
| 7161 | 623 | rides_list_0_ride_date | 18 – 03 – 2016
| 7162 | 623 | _rides_list_0_ride_date | field_56d419de62d2f
| 7169 | 623 | rides_list_2_rider_name | Test3
| 7170 | 623 | _rides_list_2_rider_name | field_56d419a662d2d
| 7171 | 623 | rides_list_2_rider_email | [email protected]
| 7172 | 623 | _rides_list_2_rider_email | field_56d419c762d2e
| 7173 | 623 | rides_list_2_ride_date | 31 – 03 – 2016
| 7174 | 623 | _rides_list_2_ride_date | field_56d419de62d2f
My current version of ACF is 5.3.5
Hi @oleg-melnyk
The delete_row() function will delete the chosen row, but won’t re-index the rest of the data. If you want to do it, you need to use the update_field() function instead. It should be something like this:
$field_key = "rides_list";
$post_id = $_POST['post_ID'];
$value = get_field($field_key, $post_id);
unset($value[$_POST['row_id']-1]);
$value = array_values($value);
update_field( $field_key, $value, $post_id );
I hope this helps.
You must be logged in to reply to this topic.
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.