You can try using the function get_attached_media() like this:
add_action( 'before_delete_post', 'wps_remove_attachment_with_post', 10 );
function wps_remove_attachment_with_post( $post_id ) {
/** @var WP_Post[] $images */
$images = get_attached_media( 'image', $post_id );
foreach ( $images as $image ) {
wp_delete_attachment( $image->ID, true );
}
}
Note that this will permanently delete all the image files related to this post. If those attachments are used somewhere else as well, those links will be broken.
Related Posts:
- Errors when uploading images in WP 3.5
- media_handle_upload for local files?
- How do I detach images from posts?
- How do I use element instead of tags in WordPress post content having webP support?
- upload featured image from front end using wordpress add media button
- Show featured image next to post-teasers in Genesis Framework?
- Set size for attachments
- wp-cli post create & media import issues
- Get all images in post and comments like Twitter before post title
- Issue where WP Featured Image will not display
- preg_replace not removed “class”
- how to give multiple post thumbnails to a post
- How to display post title inside thumbnail?
- How to get all inline images in post content? [duplicate]
- Inserting Media to WordPress Posts
- Only show featured image on exerpt and exclude images in post
- Site/Post preview is not working on all social media
- Delete media from the post but keep my featured image and post
- unable to write to the database while uploading images
- Thumbnails and images in the blog post are not loading
- Post images are not displayed in media library
- Blog featured images disappeared on my homepage
- Center crop the feature image
- Add another feature image box
- How to show category image if no featured image is set?
- Set featured image on creating new post
- Customizing individual images on posts
- WordPress Media URLs?
- Latest news mini images
- How would I attach media/images to a post based on a ID stored in a each post’s custom field
- Post thumbnail height problem
- Import alt text in set_post_thumbnail
- Broken images after importing posts and opening in Gutenberg
- website images getting corrupt automatically
- Optimize blog that serves hundred of images and videos
- Images being uploaded via WP Media are not attached even they are used by some CPT posts
- ZIP up all images displayed in a [gallery] and offer as download link
- Custom Field in Featured image for A particular post
- Add existing images from Media Library in to custom gallery
- Replace image attributes for lazyload plugin (data-src)
- How do I sort (order) the results from `get_children()`?
- upload image in a meta box
- Get post embedded image caption
- Restrict access to the Trash folder in Posts
- How do I programmatically add an image to a post?
- Post taxonomy from exif data
- Underscores.me retrieve next / previous post thumbnail in post_nav function
- get custom image size
- Thumbnails are bigger in size than the original image
- how to replace embedded “full” sized images within a post with the “large” ones
- Batch process: remove first image from post content
- Modify WP_Post before processing
- Admin Notices don’t display on the admin screen for certain post
- Can I use images as anchor tags with
- Too many connections to server
- How can I get the media inside a post?
- Set spesific size of featured images
- List all categories with featured image from post?
- Export xml use/import images from theme directory
- Display a mixed list of posts and media
- Posts page not showing correct image
- Images showing up in Post Editor, but not in Published Post
- Using wp-cli and delete everypost first image
- How to retrieve certain number of images from a wordpress post?
- Get featured image on hover of post title [closed]
- Change the background of each post according to the category
- Remove Featured Image from posts in specific category?
- Attachment image single page
- How to change the order of elements within a post?
- How to call WordPress First Image Post or else Attached Image or Default
- Regarding Posts
- Delete Post but retain image of post [WP REST API]
- Getting image data from phone camera (MediaDevices.getUserMedia) into user post
- Featured image error after importing blog posts
- How do I fix broken links to images in imported posts’ post_content?
- Manipulating images inside post content
- Output wp_link_pages as raw url
- Newly uploaded images are not updating in featured image
- Embed image in post from external url
- Is possible to do this from function.php
- Disable Media uploader to users
- SQL query to remove first image in all posts
- WP Gallery Image Page Issue When 2 galleries share one image
- Why can’t I see my attachment page information for an image?
- Wanting to apply different featured images for each post
- Specific Post Format Image Thumbnail
- Extra Theme – Fit image inside of Featured Post Slider
- Is there any SEO impacts on unattached vs attached images for a post in WordPress?
- Front end post or photo or both
- Displaying all images from a WordPress post (including media library)
- How to move all images in a post to a single gallery for a bunch of posts?
- What is the proper use of guid for images in wp_posts?
- Images and menu links disapearing after saving
- Changable favicon based on post-featured image
- Redirection – bulk edit 404s to new tomain
- WordPress portfolio posts: media not displaying if several images
- How do I display a PDF thumbnail as a link to the PDF without uploading the image
- Set Featured Image of a post
- Featured Image add tab
- How to delete Full Size options from post Add Media?