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');
How about this? It adapts an example on the get_posts() function reference page.
I suppose you’re looking for something like this…?