How to Remove Outdated Images from WP All Export
Stop WP All Export from exporting images that were deleted from a vehicle but left behind in the Media library, by installing a small cleanup plugin that clears them before every export.
1 minute read
When exporting images with WP All Export, you may notice that deleted vehicle images are still there. A vehicle with 17 images can show 5 extra images in the export file, because those images were once associated with it and are still in the WordPress Media library. The result is incorrect image counts and incorrect exports.
Solution
Follow these steps to clean up the residual images and get accurate exports.
Create the cleanup plugin
-
1
Open the plugins folder
Navigate to the
wp-content/pluginsdirectory in your WordPress installation. -
2
Create the plugin file
Create a new file named
vehica-cleanup.php. -
3
Paste in the code
Paste the code below into the
vehica-cleanup.phpfile.
<?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);
}
}
});
Install and activate it
-
1
Open the Plugins screen
Go to your WordPress admin panel (
wp-admin), then to the Plugins section. -
2
Activate Vehica CleanUp
Find the plugin named Vehica CleanUp and activate it.
Once activated, the plugin performs the cleanup automatically before each export, removing the residual images.
Ask the people who build Vehica
We answer by email from support@tangiblewp.com. With Premium Support, we also fix your site when something breaks.
We can also run your site for you, or build a new one.
Was this guide helpful?