0

Currently, when I delete a post in my wordpress website, the image is deleted automatically.

Which is fine for most cases, however, there are certain posts where I don't want to delete the image from the database such as a post that has the plain website logo (default background image when subscribers don't post image). In such case, how can I delete the post while keeping the image.

I'am using the wordpress rest api and issuing the following request (in python) to delete the post.

url='https://mywebsite.com/wp-json/wp/v2/posts'
post_id=22334

r = requests.delete(url + '/' + str(post_id) + '?force=True')

Any suggestions?

Possible solution routes:

-Is there a way to mark/protect an image from auto deletion when post is deleted

-Is there a way to instruct wordpress not to delete featured media when deleting the post

This is the deleteimage function used by the custom theme when a post is deleted:

public function deleteImages(Post $post)
{
    foreach (themeApp('gallery_fields') as $galleryField) {
        /* @var GalleryField $galleryField */
        foreach ($galleryField->getValue($post) as $imageId) {

            wp_delete_attachment($imageId);
        }
    }
}

RESOLVED: Issue was due to custom theme not designed/configured properly. Hence I modified the deleteImages function in the theme files to check if image is used more than once (if used once delete image, if more than once then dont delete image):

public function deleteImages(Post $post){
foreach (themeApp('gallery_fields') as $galleryField) {
    /* @var GalleryField $galleryField */
    foreach ($galleryField->getValue($post) as $imageId) {


        $args = array( 'numberposts' => -1, 'fields'=>'ids');
        $array2 = get_posts( $args );

        foreach($array2 as $item){
            $images = get_post_meta( $item,'customfield_19213');#this is the custom field corresponding to image id

            $imagelist[]=$images;
        }

        foreach ($imagelist as $img){foreach ($img as $im){
            $imageflattened[]=$im;
        }
        }
        $imageflattened=array_count_values($imageflattened);
        $ImageID=strval($imageId);
        $count= $imageflattened[$ImageID];

        if ($count==1){ wp_delete_attachment($imageId);}
    }
}
}
15
  • 1
    "when I delete a post in my wordpress website, the image is deleted automatically" - if by "the image", you meant the featured image, then that would not happen by default, i.e. the post is deleted, but the featured image (the image file) is not deleted.
    – Sally CJ
    Apr 27, 2022 at 10:52
  • 1
    So maybe there's a plugin which is deleting the attachment and the image file itself? And if so, then you should check the plugin's documentation or ask on their support forums for assistance.
    – Sally CJ
    Apr 27, 2022 at 10:55
  • 1
    You can try deactivating all plugins for a while and see if that image is still being deleted. But have you checked the Media Library - is the image really not there anymore? How did you confirm/know the image was deleted?
    – Sally CJ
    Apr 27, 2022 at 11:10
  • 1
    Could be. You can confirm by switching to 2021 or any other default/core theme, then see if the same issue persists.
    – Sally CJ
    Apr 27, 2022 at 11:35
  • 1
    If you just want to check if the current image/attachment ID is set as the post's featured image, then you can use something like if ( get_post_thumbnail_id( $post->ID ) !== (int) $imageId ) { wp_delete_attachment($imageId); }, assuming $post is an object and that $post->ID is set. As for the "elsewhere" - "check to ensure image is not used elsewhere prior deletion" - that won't be an easy task since the image could be linked to from other posts, e.g. using the URL in an img tag.
    – Sally CJ
    Apr 27, 2022 at 12:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.