After giving it another look with some fresh eyes, I decided to add a third loop within Loop 2 and not have it working as desired:
$media_args = array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'products',
'field' => 'term_id',
'terms' => $product_tag
)
)
);
// Loop 1 - To get the images with the desired tags
$media_items = new WP_Query($media_args);
if ($media_items->have_posts()) {
$parent = array();
while ($media_items->have_posts()) : $media_items->the_post();
// getting the post_parent and cleaning to weed out duplicates
$parent[] = get_post_field('post_parent', $post->ID);
$parent_clean = array_unique($parent);
endwhile;
}
$product_post_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post__in' => $parent_clean
);
// Loop 2 - Get the posts which have the images from Loop 1 as attachments without duplicating the same post
$product_posts = new WP_Query($product_post_args);
if ($product_posts->have_posts()) {
while ($product_posts->have_posts()) : $product_posts->the_post();
$product_image_args = array(
'post_type' => 'attachment',
'posts_per_page' => 1,
'post_parent' => $post->ID,
'tax_query' => array(
array(
'taxonomy' => 'productcats',
'field' => 'term_id',
'terms' => $product_tag
)
)
);
// Loop 3 - To get the first attachment with the desired tag for each post
$product_images = new WP_Query($product_image_args);
while ($product_images->have_posts()) : $product_images->the_post();
$img = wp_get_attachment_image_src( $post->ID, 'browse' );
echo $img[0];
endwhile;
endwhile;
}