Skip to content

Instantly share code, notes, and snippets.

@JackuB
Created October 25, 2013 14:29
Show Gist options
  • Save JackuB/7155562 to your computer and use it in GitHub Desktop.
Save JackuB/7155562 to your computer and use it in GitHub Desktop.
WordPress: find all post thumbnails that match criteria // remove those unfitting
<?php
$args = array('posts_per_page' => -1); // get all posts
$attachments = get_posts( $args );
$allP = 0;
$wThumb = 0;
$great = 0;
$ok = 0;
$wrong = 0;
if ( $attachments ) {
foreach ( $attachments as $post ) {
$allP++;
if(has_post_thumbnail()) {
$wThumb++;
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$size = getimagesize($url);
if($size[0] >= 710 && $size[1] >= 355) {
$great++;
} elseif($size[0] >= 330 && $size[1] >= 165) {
$ok++;
} else {
$wrong++;
if($size[0] == 0) {
echo "Deleted post thumbnail for <a href=\"" . get_permalink() . "\">" . get_the_title() . "</a> because post thumbnail couldn't be found<br />";
} else {
echo "Deleted post thumbnail for <a href=\"" . get_permalink() . "\">" . get_the_title() . "</a> because post thumbnail had size " . $size[0] . " × " . $size[1] . "<br />";
}
//delete_post_thumbnail($post->ID);
}
}
}
echo "found <strong>" . $allP . "</strong> posts<br /><br />";
echo "found <strong>" . $wThumb . "</strong> posts with thumbnails<br /><br />";
echo $great . " are ok<br />";
echo $ok . " are ok (for post listing)<br />";
echo $wrong . " are too small or missing<br />";
}
die;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment