Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

Howdy_McGee on "Delete Images Attached To Page / Post"

$
0
0

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 all articles
Browse latest Browse all 8245

Trending Articles