The image attachment type is stored in the postmeta table in the _wp_attachment_metadata
meta key. The data is serialized, so it can’t be filtered on. But you could use a meta_query in your query:
$images = get_posts( array(
'post_type' => 'attachment',
'orderby' => 'rand',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_wp_attachment_metadata',
'value' => 'extra_large',
'compare' => 'LIKE'
)
)
) );
foreach ($images as $img) :
$image = wp_get_attachment_image_src( $img->ID, 'extra_large' );
if ( !empty($image) )
$carousel[] = array($img->post_parent, $image);
endforeach;
That would be much more efficient — because it should only retrieve the attachments that have the ‘extra_large’ version.
Alternative Option:
Attach the images to a post, and then just use post_parent
in your query, as is shown in the answer of this question: Get all image from single page