从ACF转发器字段中删除行

时间:2018-10-14 18:08:18

标签: wordpress advanced-custom-fields

我有一个按钮,当您单击它时,它会触发ajax调用,该调用应基于数据属性删除acf转发器字段中的行。

这是函数,但有问题的行不会删除:

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 个答案:

答案 0 :(得分:0)

这没有很好的记录;您需要告诉ACF您正在传递用户ID,而不是帖子ID。为此,请将user_附加到变量中:

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;