Last active
October 16, 2021 11:09
-
-
Save seebeen/f4cb6a632123e9f03309f4ff3043c173 to your computer and use it in GitHub Desktop.
WordPress - Remove images older than three months
This file contains 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
<?php | |
global $wpdb; | |
$attachment_ids = $wpdb->get_results( | |
"SELECT | |
ID | |
FROM | |
{$wpdb->posts} | |
WHERE | |
post_type = 'attachment' | |
AND post_date >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH) | |
AND ID NOT IN | |
( | |
SELECT | |
meta_value | |
FROM | |
{$wpdb->postmeta} | |
WHERE | |
meta_key = '_post_thumbnail' | |
)" | |
); | |
$attachment_ids = array_map(function($row) { | |
return $row->ID; | |
}, $attachment_ids); | |
foreach ($attachment_id as $attachment_id) { | |
$image_url = wp_get_attachment_image_src($attachment_id, 'full')[0]; | |
echo "Redirect 410 {$image_url}\n"; | |
wp_delete_attachment($attachment_id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment