When exporting images using WP All Export, users may notice that deleted vehicle images remain in the system. For example, a vehicle that has 17 images may also show 5 additional images when export file. It is because images were previously associated with it and are stim in WordPress Media. This can lead to incorrect image counts and exports.

Solution

Follow these steps to clean up residual images and ensure accurate exports:


Create and Install the Cleanup Plugin

1. Navigate to the wp-content/plugins directory in your WordPress installation.

2. Create a new file named vehica-cleanup.php.

3. Paste below code in into the vehica-cleanup.php file

<?php
/*
Plugin Name: Vehica CleanUp
Version: 1.0.0
Plugin URI: https://tangiblewp.com
*/
 
add_action('pmxe_before_export', function () {
    $attachments = new WP_Query([
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'posts_per_page' => -1,
    ]);
 
    foreach ($attachments->posts as $attachment) {
        /* @var WP_Post $attachment */
        $assignedPostId = wp_get_post_parent_id($attachment->ID);
        if (!$assignedPostId) {
            continue;
        }
 
        $model = \Vehica\Model\Post\Car::getById($assignedPostId);
        if (!$model instanceof \Vehica\Model\Post\Car) {
            break;
        }
 
        $found = false;
 
        foreach (vehicaApp('gallery_fields') as $galleryField) {
            /* @var \Vehica\Model\Post\Field\GalleryField $galleryField */
            $imageIds = $galleryField->getValue($model);
            if (!in_array($attachment->ID, $imageIds, true)) {
                continue;
            }
 
            $found = true;
        }
 
        if (!$found) {
            wp_delete_attachment($attachment->ID, true);
        }
    }
});

4. Go to your WordPress admin panel (wp-admin), then to the 'Plugins' section.

5. Find the plugin named Vehica CleanUp and activate it.

6. Once activated, the plugin should automatically perform a cleanup before each export, removing residual images.