Support

Account

Home Forums Add-ons Repeater Field delete_row() function leaves empty row

Solved

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.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘delete_row() function leaves empty row’ is closed to new replies.