Deleting images attached to a post
-
I would like to have all the images that are attached to a post or custom post type be deleted when my client deletes a post.
I understand that images may be attached to more than one post, but in my situation that is not the case at all.
I have seen and tried this solution:
function delete_post_children($post_id) { global $wpdb; $ids = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_parent = $post_id AND post_type = 'attachment'"); foreach ( $ids as $id ) wp_delete_attachment($id); } add_action('delete_post', 'delete_post_children');
but it doesn’t seem to work.
Also, manually deleting unattached images just isn’t a viable option.
Any help would be greatly appreciated 🙂
- The topic ‘Deleting images attached to a post’ is closed to new replies.