simpliko / wpadverts

WordPress Classifieds Plugin
https://wpadverts.com/
GNU General Public License v2.0
20 stars 11 forks source link

Automatically delete Advert images #150

Closed erikdemarco closed 2 years ago

erikdemarco commented 2 years ago

In v1.5.8 there is new feature to to automatically delete Advert images from Media Library when Ad is permanently removed.

But after testing, it will remove only 5 attachments

See this line: https://github.com/simpliko/wpadverts/blob/43025dc7cf26ab77db21978595e29c055eee6c88/includes/functions.php#L3899

get_posts by default will only return 5posts. see: https://developer.wordpress.org/reference/functions/get_posts/#parameters

And the correct way to delete attachment is by using wp_delete_attachment() instead of wp_delete_post, as it will also do additional cleaning process for attachment post. like deleting the actual file for example.

More better version something like:

    $attachments = get_attached_media( '', $post_id );
    if( is_array( $attachments ) ) {
        foreach ($attachments as $attachment) {
            wp_delete_attachment( $attachment->ID, 'true' );
        }
    }