Looking at @obmerks answer on https://stackoverflow.com/questions/12997698/delete-images-after-post you can simplify it with this:
foreach ( $productIds as $productId ) {
$child_atts = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_parent = $productId AND post_type="attachment"");
foreach ( $child_atts as $id ) wp_delete_attachment($id,true);
}
Note: as per documentation https://codex.wordpress.org/Function_Reference/wp_delete_attachment set the second parameter to true to force delete the attachment, otherwise it goes to trash.
Related Posts:
- How do I delete all UNUSED images from my uploads directory?
- Woocommerce featured image of page – not product
- wordpress (woocommerce) add image to media library programmatically python
- Resize into square the single product image in woocommerce without cropping [closed]
- How to make footer area transparent? Using Avada and WordPress [closed]
- Product Images Low Quality After Updating From WP 3.5 to 4 [closed]
- Hide image gallery in Woocommerce if NO image [closed]
- When upload a PNG a noise appears in the thumbnails
- Exclude first image from a Woocommerce image gallery
- Why does wp_get_attachment_image_src return the wrong size, when the correct size exists?
- How to delete images from database with product
- WooCommerce Variation Swatches and Photos – get attribute thumbnail
- How can the alt be added to this wooCommerce category image?
- Bypass wp_safe_remote_get()?
- Image thumbnail on single product issue | WooCommerce
- Thumb doubles the image file size
- get attachment title based on attachment id
- Get Image Description
- PHP Fatal error: Call to undefined function download_url()
- Change WordPress image URLs via filter
- hook into completed image upload filter
- Strange img srcset behaviour
- Images uploading to wrong folder
- set_post_thumbnail_size not cropping featured images, but reducing proportionally
- How to Make WordPress Default gallery responsive on mobile?
- Why does wordpress generate more thumbnails than the 3 standard sizes?
- Pulling Post Image Attachments in WordPress 3.9
- How to add an image for unit testing?
- including image assets in widget
- Link Featured Thumb to Attachment Page, If Possible
- Different captions for the same image in multiple galleries. Edit Image in Gallery locally
- Is there a shortcut to change bulk BMP images to JPG images in a site?
- Is 700,00 jpgs too many for a WordPress site.?
- Change the RSS feed image
- WordPress Post featured image URL in the header
- Add instructions to featured image
- How do I get more image editing options in the admin?
- HTTP Error when uploading files above 7mb unless using GD Image Editor
- Question about add_image_size()
- Image resize using url parmater
- How to display / include a specific jpg on a page
- Grab remote images and update links in existing posts
- Store Snapshot created via wordpress to use with lightbox
- Get image URL after image created from canvas and sent to WP uploads with AJAX
- How can I use cached images in an AJAX response?
- Can’t get images to align with Gutenberg editor
- Show image EXIF info
- Retrieve image from content, assign it as a featured, and then remove it from the content
- Custom Loop, Match Category with Page: How to display post featured image?
- Modifying the HTML Output of Inserted Images
- Redirect to another page using contact form 7? [closed]
- Google verification challenge with file location
- How to check for images before echo
- Remove height attribute from wp_get_attachment
- How do I retrieve an image’s width and height using Advanced Custom Fields?
- wp get attachment image always gives me thumbnail
- Two “the_excerpt” questions
- Images are not showing after moving WordPress installation from server to localhost?
- Images are not showing up in “Uploaded to this post”
- Cache DB queries result
- Remove “%” from strings in serialized arrays in wp_postmeta
- Images not found after migrating WordPress website
- Adding srcset to an image in ajax
- Error uploading images in wordpress
- Disable Media File Image Link On Mobile Devices
- Set two default featured images
- Automatically generate missing images
- Apply nextgen-gallery lightbox effect [closed]
- VT-resize (great alternative to timthumb) : Errors cropping (litespeed, centos 6.3)
- Allow admin roles to add images to comment replies
- Page speed error: “serve scaled images”
- Image Captions Have a 10px Extra Margin, And It’s Not CSS? [duplicate]
- Image.php Problem – Post Images Not Displaying In It
- How can i sorting and display image order by siz in media library?
- x2 images for retina?
- Load url image full size in thumbnail, medium, large size
- Correct method of reducing the number of images created by WP and Woo together
- Images not loading on wordpress.org site
- WordPress can’t detect any thumbnails size?
- Remove image border in twentyeleven theme [closed]
- second featured post not displaying on page [closed]
- WordPress not choosing the right image from srcset
- how to remove / hide ‘featured image’ option from side menu
- 403 Forbidden when accessing image in wordpress
- For what does WP generate Thumbnails?
- Images are not shown
- Adding multiple images on my site – without using plugins [closed]
- Remove the height limit on wordpress 3.4.2
- Missing Images in Uploads Directory (Server Only)
- Image media upload problem in page editing modus?
- Is there a good way to use CMS images with CSS
- Upload image from other server without resize?
- Forcing absolute src links in the RSS feed
- where is images/image.jpg?
- Using the on-board image editor for featured images: edits are not being used
- change with CSS the color of an svg background image set with “background-image: url()”
- Img alt tag showing different value then entered
- What type to upload image within theme options?
- Uncaught Error: Call to undefined function file_is_valid_image()
- Remove one srcset entry from wp_get_attachment_image