0

I've tagged a number of media uploads on my site with the various products' names which are featured in them. The goal is to get a list of the posts which have been tagged with each product name, but without duplicating the parent post.

Ideally I would be able to display one of the attachments from Loop 1 (which actually displays the tagged product) with a link to its post_parent, but I have only been able to get the post thumbnail from Loop 2 (these don't always show the product) to work up to this point.

My brain is fried. Any thoughts?

Loop 1 - querying for attachments with a particular tag.

$media_args = array(
    'post_type' => 'attachment',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'products',
            'field' => 'term_id',
            'terms' => $product_tag
                    )
                )
            );
$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;
}

Loop 2 - querying for the list of unique posts

$product_post_args = array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'post__in' => $parent_clean
    );
$product_posts = new WP_Query($product_post_args);

if ($product_posts->have_posts()) { ?>
   while ($product_posts->have_posts()) : $product_posts->the_post();

   $img = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'browse' );

   echo $img[0];

   endwhile;
   } 
0

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;
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.