1
votes

I have a button that when you click it, it triggers an ajax call to a function that should delete a row in a acf repeater field based on a data attribute.

Here's the function, but row in question doesn't delete:

add_action('wp_ajax_nopriv_remove_from_playlist','remove_from_playlist');
add_action( 'wp_ajax_remove_from_playlist', 'remove_from_playlist' );

function remove_from_playlist() {
    $field_key = 'field_5b6b8c16eb66b';
    $userID = $_POST['user'];
    $lessonID = $_POST['lessonID'];

    if( have_rows('user_playlist', $userID) ):
        while( have_rows('user_playlist', $userID) ) : the_row();
            $value = get_sub_field('post_id');

            if($value === $lessonID) {
                $row = get_row_index();
                delete_row($field_key, $row, $userID);
            }
        endwhile;
    endif;
    die();
}
1

1 Answers

1
votes

This is not very well documented; you need to tell ACF that you are passing a user ID, not a post ID. To do this, append user_ to your variable:

if( have_rows('user_playlist', 'user_' . $userID) ):
    while( have_rows('user_playlist', 'user_' . $userID) ) : the_row();
        $value = get_sub_field('post_id');

        if($value === $lessonID) {
            $row = get_row_index();
            delete_row($field_key, $row, 'user_' . $userID);
        }
    endwhile;
endif;