Is there any solution for – deleting posts also deletes image attached to it??
-
Hi All,
I try to find answer to this problem all over the internet and no success.
Is there any solution for – deleting posts also deletes image attached to it?? – like featured images in the media.Thanx in advance.
AB
-
Can you clarify the question a little more? I assume you mean you’d like featured images to be deleted once the attached post is deleted?
David.
Yes, when i delete the post, the images are not deleted in the media (i mean related images to the post), plus when i deleted them manually, there are many differently sized cropped images left in the upload folder on the server.
Thanx
Ok, I see. Well, it’s not an issue per se but how it works. I don’t know the exact details as to why, but I suspect it’s to do with the fact that you could easily be sharing these images elsewhere. So, for my own site, I often share images across posts, sometimes for featured images and sometimes elsewhere – if I deleted a post with a particular featured image it would cause issues if the images were then deleted.
David.
Thanx david, actually my site is coupon website, therefore after expiration date, actually there is no need of the featured image of the coupons as well as the post. Maybe WordPress is not meant for it.
AB,
Therefore it become quite manual, do delete the respective cropped images using plugin called Image Cleanup. Therefore I thought of maybe there is some hack that can delete the photos, when I delete the coupon posts.
David summed up the problem nicely, but developing something might still be possible, depending on how you utilize images on your site. Even so, there are several related issues that should be considered, so any solution will likely be less than ideal.
If no one ever reuses existing images, you could hook ‘before_delete_post’ and delete all attachments to the post.
wp_delete_attachment()
will remove the related image files. One issue here is you have to get the attachments before deleting the post because if you wait until after the post is deleted, you cannot know which attachments belonged to the post. By deleting early, should the post deletion fail, the attachments are still lost.Even if images are sometimes reused, you could probably get a list of image filenames for each attachment and query all post content for the presence of one of those file names. If none are found, it’s probably safe to delete the files.
It’s also possible for images to appear in a post without a related attachment, as when an existing image is reused. It may be legitimate to delete these image files, like if the related attachment has no parent, or if there is no related attachment, and the the filename does not appear in any other post content.
A lot of things to check for, but the possibility of auto-deleting associated image files does exist.
Thanks bcworkz. I like, occasionally, to work on a plugin that I know only a few will need so I’ll add this to my list of possibilities.
@anandmongol – don’t get excited, I’m not writing a solution for you. It may be months, longer or never before I create it!
When I posted my original response I did a quick search and there are quite a few housekeeeping plugins that clear down these image files, so it may be worth having a look at that as a solution. I think the overhead to do this kind of checking for each post deletion would be too much, hence the batch plugin solution.
David.
I am looking for the same and I got in the right direction, but wasn’t able to do it completely.
This script unattaches images when a post is deleted. So you can easily delete all unattached images.
According to another topic it should also delete the attachements (if you uncomment the appropriate line), but I haven’t been able to do so.
function delete_associated_media($id) { // check if page if ('your_post_type' == get_post_type($id)) { $media = get_children(array( 'post_parent' => $id, 'post_type' => 'attachment' )); if (!empty($media)) { foreach ($media as $file) { // pick what you want to do unlink(get_attached_file($file->ID)); // wp_delete_attachment($file->ID); } } } } add_action('before_delete_post', 'delete_associated_media');
- This reply was modified 8 years, 2 months ago by Beee.
I found out why it wasn’t working. You have to use one of them, not both.
Now first the image becomes unlinked and then it can’t be found anymore since it’s not connected to the post.So just delete the unlink line and uncomment the wp_delete_attachement line.
Thanks for that @beee.
Can I assume if an image is shared on multiple posts then this will still delete it?
It will only delete the images attached to $file (which holds the post id).
It takes the post id, retrieves all images which are linked to that specific post (which post this is can be seen in the media editor).
Hope that clears it up.
So, yes, in that case. If you attach an image to a post and then use it in another post it will break the other post if the first one is then deleted. I just need to make sure everyone is aware of the consequences of this.
Yes correct. WP doesn’t know if you inserted the images into another post. I only sees to which post the image is connected, which is stored in wp_posts, in the column post_parent for that specific attachement (which is actually a post, which has post_type attachement).
- This reply was modified 8 years, 1 month ago by Beee.
Now, if you could do just that – check for usage elsewhere – we’d have a pretty robust solution.
That’s a whooooooole ‘nother step. Because you have to search all posts with a regex to see if a certain string exists in the post_content.
Not something I have use for or the time to look into. So I can’t help you there. Good luck.
Does it not register as an attached image to the other post as well? I’m thinking if I use the Attach Media option in a post to add in an image and then do the same in another post. Won’t both posts have the same image registered? In which case we’d just need to crawl all the posts to see if the same image exists, wouldn’t we?
- The topic ‘Is there any solution for – deleting posts also deletes image attached to it??’ is closed to new replies.