Is there a good way to know if a post has an image attachment of a particular size? I'm trying to fetch 5 random posts, but I only want results that have image attachments with a (custom) size of "extra_large".
$images = get_posts( array( 'post_type' => 'attachment', 'orderby' => 'rand', 'posts_per_page' => -1, ) ); foreach ($images as $img) : $image = wp_get_attachment_image_src( $img->ID, 'extra_large' ); if ( !empty($image) ) $carousel = array($img->post_parent, $image); endforeach;
...but I don't particularly like this as I have to grab every single image, and then loop through them to see if there's an extra_large version. I'm just using the first 5 items in $carousel, but as my data set is very large there's a lot of un-needed processing. Also, I then have to do another call to get the parent from the ID.
Is there a more elegant way to do this?