Removing Images from a WordPress Post
delete_attachment( $attachment_id, true );
wp_delete_post( $attachment_id, true );
Are your best friends. You will need something like this:
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID,
'post__not_in' => array(
get_post_thumbnail_id( $post->ID )
),
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
delete_attachment( $attachment_id, true );
wp_delete_post( $attachment_id, true );
}
}
The “true” statement defines that the attachments and their IDs are completely removed from the backend and filesystem. If you want to store them into the trash, then set it to false.
Related Posts:
- Check if page/post has any anchors that link to an image jpg/gif/png
- image resizing on post content without css and html tags
- I tried to move the featured image below the paragraphs but now it is displaying twice
- WP 4.4. responsive images browser choosing the “wrong” one
- Retina images – custom syntax for inserting images into post content
- show author image in posts
- Default Image Link Removal
- Replace Woocommerce Images
- How to copy and paste a picture found on the web to wordpress easily
- Graphing libraries for WordPress [closed]
- add_image_sizes ignoring crop sizes and using proportion
- Dynamically add titles to post images if they are empty
- get_intermediate_image_sizes Get the Size Names but How Do I Get the Sizes?
- Cropping an image before inserting into a post
- custom image size with New Media Manager in wordpress 3.5
- Uploading images: ‘ø’ get replaced with ‘ø’
- SSL: How to make customizer images Protocol Relative in WordPress?
- How to add qtranslate multi language support for media?
- Change the URL of an image from wp_get_attachment_image_src
- custom image dimensions (for gallery)
- Screenshot.png not showing up
- Do I need to resize an image to fit the post?
- Some images not found 404 in localhost
- wp_upload_bits does not retrieve images that do not have an extension
- Is there any action/filter hook to use when an image gets edited in the editor?
- Images will not display, when I call them up with html in wordpress, just two blank small spaces [closed]
- Force minimum image size and aspect ratio when uploading images
- Changing default img html markup but not in admin back-end?
- Is it possible to split Default WP Media directory to multiple Folder/Subdomain?
- Add srcset and sizes to older uploads?
- Change align classes for images
- Ho to add “Create Slider” option to default “Add Media” popup?
- Divi Theme – images not showing on front page [closed]
- Edit image results in copies of file being created
- Images in Media aren’t showing, not broken
- Fixing image names for a site being restored
- Upload Image using wp_insert_attachment and file url don’t have file extension
- Export xml use/import images from theme directory
- Custom header tiling?
- Using featured img as div background
- Images showing in editor but not in post [closed]
- Why are featured images sometimes cropped, and sometimes not
- Display an icon with get_post_meta?
- Include WooCommerce 2’s prettyphoto scripts on all site pages
- Get custom thumbnail sizes [duplicate]
- Where do I locate theme image files
- How to set uploaded image as featured image wordpress
- Random background image showing on website per click/load
- Warning : tempnam() [function.tempnam]: SAFE MODE Restriction in effect
- How to force WordPress to upscale images?
- How to stop WordPress from completely overriding my tags in my templates?
- Image text wrap not working properly on frontend
- How can i add a section of category image upload during adding of category in wp admin area?
- remove enclosure url from feed
- Show last 12 uploaded images on home page, but only from posts
- Image and shortcode not showing in excerpt
- Emoticons not working on my site
- How to use getimagesize() of image upload?
- problem with add_size_image
- photo gallery implementation like tmz
- Graphics and Formatting Not Loading in WordPress on IIS
- How to Automatically import external images to Upload
- How to enlarge thumbnail on mouseover?
- Edit Page did not get refresh after update
- get_post_gallery with Gutenberg
- How to join Caption AND Description Meta Fields as one combined-caption in Image Block
- Images not showing after wordpres update 5.6
- How to avoid images appear as post in google search?
- Images names not inserting in WordPress Database from Dynamic Add / Remove fields
- Images do not show in the website, but they appear on new tab
- Generate links on all images in posts – is there a hook?
- Decide how Hero image appears on mobile
- Image linking and lightbox
- Which tools should I use for a gallery that uses an API and url for the path of the images?
- Uploaded images not showing properly using wordpress
- Can’t figure out how to get space around an image? [closed]
- Uploaded images appear broken in WP 4.4
- How to best adjust images to a max content width of 490px?
- Set WordPress to Create different images format later, not during upload
- Get attachment images does not return any attached images
- How might one programmatically set the link for all images in all posts?
- attachment.php & flexslider—linking thumbnail to specific image
- page displaying media caption shortcodes
- Display different Images depending on the current day
- How to associate an image with a term taxononmy and publish it on frontpage?
- How to add a credit line to a photo caption
- How can i make like this structure?
- Slider do not add alt attribute. Cant get alt attribute from a media library!
- Broken image URL
- Stop Attachment image names from taking top level permalink
- Image not showing in an archive page
- Media upload error
- Featured Image not displaying [closed]
- Remote Image (from URL) as Media Library element
- Theme using masonry layout script rendering overlaped images in Google Chrome [closed]
- WordPress update leads to 404 error on admin page and signup. CSS mishap with all plugin related functions
- How to get dimension of Logo and Crest
- WordPress Missing Images
- Display first image’s caption
- The uploaded images are only full-size (original)