• When I delete a page or post I want to delete all images (from the media library) that are attached / associated with said page or post. I’ve hooked into Delete Posts and tried to query all attached images so I could delete them, but no luck. Anybody have suggestions?

    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’);

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Howdy_McGee

    (@howdy_mcgee)

    Anybody reading this in the future… Hello from the past ??

    Here’s some answers:

    Thanks to /u/Charles Clarkson

    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');

    Thanks to /u/t f

    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');

    https://wordpress.stackexchange.com/questions/109793/delete-associated-media-upon-page-deletion

    Elron

    (@elron)

    Charles Clarkson’s code worked perfectly, I just added some protection to it.

    function delete_post_media( $post_id ) {
    
    	if(!isset($post_id)) return; // Will die in case you run a function like this: delete_post_media($post_id); if you will remove this line - ALL ATTACHMENTS WHO HAS A PARENT WILL BE DELETED PERMANENTLY!
    	elseif($post_id == 0) return; // Will die in case you have 0 set. there's no page id called 0 :)
    	elseif(is_array($post_id)) return; // Will die in case you place there an array of pages.
    
    	else {
    
    	    $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.
    	        }
    	    }
    	}
    }
    
    /******* DANGER - WILL REMOVE ALL ATTACHMENTS FROM THIS PARENT ID (666) *******/
    // delete_post_media( 666 );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Delete Images Attached To Page / Post’ is closed to new replies.