Last active
November 2, 2018 14:10
-
-
Save cmdmcs/1c722b4574eba766b89cf8480fc6ac09 to your computer and use it in GitHub Desktop.
delete images that have been unattached from properties (as they are no longer in the import file)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter('wp_all_import_is_post_to_update', 'wpai_wp_all_import_is_post_to_update', 10, 5); | |
function wpai_wp_all_import_is_post_to_update($continueImport, $pid, $currentXmlNode, $importID, $simpleXml) { | |
$images = get_attached_media( 'image', $pid ); | |
$ids = []; | |
foreach ($images as $image) { | |
$ids[] = $image->ID; | |
} | |
update_option('_wpai_attached_images_before_import_' . $pid, $ids); | |
return $continueImport; | |
} | |
add_filter('pmxi_saved_post', 'wpai_pmxi_saved_post', 10, 3); | |
function wpai_pmxi_saved_post($pid, $rootNodes, $isUpdate) { | |
$attached_images_before_import = get_option('_wpai_attached_images_before_import_' . $pid); | |
if (!empty($attached_images_before_import)) { | |
$images = get_attached_media( 'image', $pid ); | |
$ids = []; | |
foreach ($images as $image) { | |
$ids[] = $image->ID; | |
} | |
$missing = array_diff($attached_images_before_import, $ids); | |
foreach ($missing as $image) { | |
wp_delete_attachment( $image ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment