0

I have on my page a multi file upload that saves post attachments within a custom field ---

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++;
                    }
                }

I am trying to use a delete button and ajax on the front end to delete the post attachment (this is inside a foreach statement '$vP' is the attachment 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>';  

On my functions.php page I have -

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();
}

So far it deletes the image but it doesnt delete the attachment post. So the ID still shows up in my foreach loops etc, but the image is successfully removed.

How can I completely remove the attachment and not just the file?

1

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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.