AJAX从自定义字段数组中删除值

时间:2018-06-30 作者:730wavy

我的页面上有一个多文件上传,可以在自定义字段中保存帖子附件---

if (!empty($_FILES[\'vidPix\'][\'tmp_name\'][0])) {
                    $i = 1;
                    $files = $_FILES[\'vidPix\'];
                    foreach ($files[\'name\'] as $key => $value) {
                        if ($files[\'name\'][$key]) {
                            $file = array(
                                \'name\' => $files[\'name\'][$key],
                                \'type\' => $files[\'type\'][$key],
                                \'tmp_name\' => $files[\'tmp_name\'][$key],
                                \'error\' => $files[\'error\'][$key],
                                \'size\' => $files[\'size\'][$key]
                            );
                            $_FILES = array("sight" . $i => $file);
add_filter( \'upload_dir\', \'wpse_141088_upload_dir\' );
add_filter(\'intermediate_image_sizes_advanced\', \'no_image_resizing\');

                        $mfile =  wp_handle_upload($files, $upload_overrides );

                            $newvidPix = sight("sight" . $i, $v_Id);
remove_filter( \'upload_dir\', \'wpse_141088_upload_dir\' );
remove_filter(\'intermediate_image_sizes_advanced\', \'no_image_resizing\');

                            if ($i == 1) {
                                update_post_meta($v_Id, \'_thumbnail_id\', $newvidPix);
                            }
                                add_post_meta($v_Id, \'vid_pix\', $newvidPix, false);
                        }
                        $i++;
                    }
                }
我试图在前端使用delete按钮和ajax来删除post附件(这在foreach语句中,“$vP”是附件ID-

echo \'<input type="hidden" id="att_remove" name="att_remove[]" value="\'. $vP .\'" /><input type="hidden" name="nonce" id="nonce" value="\' . wp_create_nonce( \'delete_attachment\' ) . \'" /></div>\';  
关于我的功能。php页面我有-

function delete_attachment( $post ) {
$id = $_POST[\'att_ID\'];
$v_Id = $_POST[\'post_ID\'];
$vid_pix = get_post_meta($v_Id, \'vid_pix\', true);

delete_post_meta($id, $vP);
delete_post_meta($id, $vid_pix);
delete_post_meta($v_Id, $vid_pix, $id);
wp_delete_attachment( $id, $force_delete ); 

update_post_meta($v_Id, \'vid_pix\');
die();
}
到目前为止,它删除了图像,但没有删除附件帖子。因此,ID仍然显示在我的foreach循环等中,但图像已成功删除。

我如何才能完全删除附件而不仅仅是文件?

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

Try this:

function delete_attachment() {
    $id = $_POST[\'att_ID\']; // the attachment ID

    // Deletes the attachment (and all of its custom fields or meta data; e.g.
    // `photo_order`). The `true` bypasses the trash.
    $att = wp_delete_attachment( $id, true );

    // Deletes all custom fields named `vid_pix` where the value (`meta_value`)
    // is the attachment ID (i.e. `$id`).
    if ( $att ) {
        global $wpdb;
        $wpdb->query( $wpdb->prepare(
            "DELETE FROM {$wpdb->postmeta} WHERE meta_key = \'vid_pix\' AND meta_value = %s",
            $id
        ) );
    }

    die();
}
结束

相关推荐

delete post also attachments

我正在尝试删除包含所有附件的帖子。这就是我现在想到的函数;function remove_post(){ if(isset($_POST[\'post_id\']) && is_numeric($_POST[\'post_id\'])){ $post = get_post($_POST[\'post_id\']); if(get_current_user_id() == $pos