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
- How do you add thumbnail support for custom post types?
- ZIP up all images displayed in a [gallery] and offer as download link
- removing inline styles from wp-caption div
- Add custom Attachment Display Setting for images
- Post featured image column on admin post list page
- How can you change the ‘Insert into Post’ title in the media button?
- How to get Images included in Post
- disable WP automatically inserted line breaks after an image
- Find the post an attachment is attached to
- Delete the original big size image after upload and leave only 3 images crunched by media gallery
- Custom Field in Featured image for A particular post
- 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)
- How to add/assign or change featured image in post programmatically?
- Replacing all attachment links in post with media file link
- How to change the naming of the resized image files
- 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 remove images from showing in a post with the_content()?
- VT-resize image and display it [duplicate]
- Unattaching images from a post
- how to display post content without post image?
- How to wrap every image in a post with a div?
- How to upload imagick resource to media in wordpress
- How do I create Comma Separated list of attached image ids?
- 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
- Posts Page Featured Image [closed]
- Load post content into div with ajax
- Retrieving and Storing Images Linked from Other Domains on Local Server?
- Showing Thumbnail from Previous and Next Posts
- Undelete post (untrash)?
- How do I programmatically add an image to a post?
- How to Replace the WordPress Featured Image with a Video?
- Add an image box besides featured image?
- Front end post or photo or both
- Why some images are not allowed to be edited only for thumbnails?
- How to import external media items into wordpress media library (without using storage)
- Search doesn’t display posts with images
- Inserting Media on New Post But Hides Previous Uploads for Editor/Contributor
- 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?
- Inside post image title has been posting as a post title on my main post page
- What is the proper use of guid for images in wp_posts?
- Download button under all images WordPress
- Images and menu links disapearing after saving
- When inserting media file in a post or page, show file name under the thumbnail
- Changable favicon based on post-featured image
- Redirection – bulk edit 404s to new tomain
- how to display the actual size of the picture in post
- WordPress portfolio posts: media not displaying if several images
- How to upload multiple images to use as gallery in Kirki Framework
- How do I display a PDF thumbnail as a link to the PDF without uploading the image
- Set Featured Image of a post
- Is there a way to eliminate flyout menus when adding a post?
- Get featured image thumbnail and inserting into custom field on wordpress posts
- Undefined array key “width” in wp-admin/includes/image.php error when publishing post or page
- Featured Image add tab
- Implementing Soft Delete for Bulk Actions / Empty Trash
- How to delete Full Size options from post Add Media?