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
- Post slugs and images cannot have same name?
- Add existing images from Media Library in to custom gallery
- Replace image attributes for lazyload plugin (data-src)
- Download external images if post is publish
- How do I sort (order) the results from `get_children()`?
- upload image in a meta box
- Can I load posts via Ajax?
- How to wrap every image in a post with a div?
- Get post embedded image caption
- Restrict access to the Trash folder in Posts
- how to grab first link in post… and of course call it
- Undelete post (untrash)?
- Delete Post From Front-End and attachment permanently
- Limit function to specific post category
- Post taxonomy from exif data
- Underscores.me retrieve next / previous post thumbnail in post_nav function
- When trashing a post, also trash related comments
- how to replace embedded “full” sized images within a post with the “large” ones
- WordPress Theory Lesson – Image Columns within posts with alignment?
- Batch process: remove first image from post content
- Modify WP_Post before processing
- Get image from post’s gallery
- Image Insert into posts and pages not working after 3.2.1 update
- How to display featured image without it appearing in post?
- Can I use images as anchor tags with
- creating front end image uploader that uploads images as posts
- How can I get the media inside a post?
- List all categories with featured image from post?
- How can you change all past posts’ images to link to none instead of the image file?
- Gather posts into a cart/lightbox and share with another user
- How to check if single.php has already called the_post_thumbnail function
- Thumbnail Image to go in the post aswell
- update image path with words starting uppercase to lowercase chars
- Uploading images in custom post form
- Make insterted image point to post url instead of attachment page
- Get featured image with custom size outside WordPress
- Post Title to featured Image ALT
- How to Featured content with images in home page?
- How to show a featured image inside a post in only 1 single category
- Link post images to post
- How to get the next and previous image title from attachment or gallery?
- WordPress as CMS: How to manage/handle images assigned to a post as full-sized background images in a slideshow?
- How to exclude native lazy loading from first image in post content?
- Rewrite image links from attachment id to attachment link
- Aligning images in the wp editor not working. How do i get this to work in custom theme?
- Change the size of the featured image for post?
- Set featured image as hero image on each page
- Delete attached images from posts
- Schedule image post on download
- How to get all posts in gallery post format template
- Stop images from being displayed on the front-end of the site
- How to get only current images of a post
- Relative Image Urls in Posts are starting from root – Not from WordPress Address (URL) at General Settings
- How to associate media with a post?
- How do I get thumbnails?
- Featured Images link to post parent
- How do I insert a custom field in a user submitted post?
- thumbnail image path and file path for gallery thumbnail images
- Displaying warning if no featured image has been set – Post Editor
- Get attachment if matches a variable
- Bug with post titles [closed]
- $attachment->post_title not displaying title
- 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?