9

Note

Use at your own risk, it is buggy and I have run across a couple instances where it would delete ALL attachments. Unsure why.

Is it possible to delete media associated with a page when that page is deleted? I know in the Insert Media page you can filter by images "Uploaded to this page" so could I get a list of those and just delete them as the page is being deleted?

Right now I'm playing around with hooking into Delete Post. Right now... it does nothing but I think I'm getting somewhere with it.

function del_post_media($pid) {
    $query = "DELETE FROM wp_postmeta
            WHERE ".$pid." IN
            (
            SELECT id
            FROM wp_posts
            WHERE post_type = 'attachment'
            )";
    global $wpdb;
    if ($wpdb->get_var($wpdb->prepare($query))) {
        return $wpdb->query($wpdb->prepare($query));
    }
    return true;
}
add_action('delete_post', 'del_post_media');
  • 1
    Be careful about deleting references to media that are used elsewhere e.g. other posts or if your post has been shared on social media. – Gilbert Aug 19 '13 at 19:37
  • Well if I force the user to upload the same image twice (if they want the same image for 2 different pages) it will rename one of the images, so it should be fine. – Howdy_McGee Aug 19 '13 at 20:32
8

How about this? It adapts an example on the get_posts() function reference page.

function delete_post_media( $post_id ) {

    $attachments = get_posts( array(
        'post_type'      => 'attachment',
        'posts_per_page' => -1,
        'post_status'    => 'any',
        'post_parent'    => $post_id
    ) );

    foreach ( $attachments as $attachment ) {
        if ( false === wp_delete_attachment( $attachment->ID ) ) {
            // Log failure to delete attachment.
        }
    }
}

add_action( 'before_delete_post', 'delete_post_media' );
  • 1
    Didn't work for me. Created post attached 1 image, trashed post, empty trash. Image is still there on media library. EDIT: Oh it's missing the add_action part – Michael Rogers Apr 2 '17 at 19:03
8

I suppose you're looking for something like this...?

function delete_associated_media($id) {
    // check if page
    if ('page' !== get_post_type($id)) return;

    $media = get_children(array(
        'post_parent' => $id,
        'post_type' => 'attachment'
    ));
    if (empty($media)) return;

    foreach ($media as $file) {
        // pick what you want to do
        wp_delete_attachment($file->ID);
        unlink(get_attached_file($file->ID));
    }
}
add_action('before_delete_post', 'delete_associated_media');
  • Does nothing for me. Deleting post results on unattached media as usual it doesn't delete it. – Michael Rogers Apr 2 '17 at 19:09
  • Unless you adapted the code, it's for pages only. You saw that, right, @MichaelRogers? – tfrommen Apr 3 '17 at 11:39
  • Gotcha sorry then! – Michael Rogers Apr 3 '17 at 16:53

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.