Taxonomy archive + query attachments = duplicate results

I’m trying to get the attachments of a specific term (in its archive page). But the results are showing the resulting images 5 times instead of one.

I have multiple loops in this page – one to show related posts, another to show related products (custom post), and this one to show related images. Custom posts and posts are working nicely, but I can’t show the attachments in the right way. :S

<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id; 

$args = array(
'post_status' => 'inherit',
'numberposts' => 0,
'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
'post_type' => 'attachment',
);

$args['tax_query'] = array(
array(
    'taxonomy' => 't-arte',
    'terms' => $term_id,
    'field' => 'id',
),
); ?>

<?php $t = $data['t-arte'];
$array = explode(" ", $t);
$array = array_unique($array);?>

<?php $media_query = array_unique($array); ?>

<?php $media_query = get_posts($args);
if( !empty( $media_query ) ) :
foreach ($media_query as $media_query) :
global $post; $post = $media_query;
setup_postdata($media_query);
?>

<div id="archivespage-media-item">   

<?php $attachments = get_posts( $args );
                 if ( $attachments ) {
                    foreach ( $attachments as $attachment ) {
                       echo '<div id="imagem">';
                       the_attachment_link( $attachment->ID, true );
                       echo '</div>';
                       }
                 }?>

</div>

<?php endforeach;else :?>       

<p>Ainda não temos nenhuma imagem relacionada :(</p>  

</div>

<?php endif; ?>
    <?php wp_reset_query();?>

Related posts

Leave a Reply

2 comments

  1. I think the problem is that you’re passing the wrong arguments to the $attachments query, causing you not to get the intended posts in the $attachments query.

    Here’s what you’re doing:

    $args = array(
        'post_status' => 'inherit',
        'numberposts' => 0,
        'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
        'post_type' => 'attachment',
    );
    
    $args['tax_query'] = array(
        array(
            'taxonomy' => 't-arte',
            'terms' => $term_id,
            'field' => 'id',
        ),
    );
    
    $attachments = get_posts( $args );
    

    So, you’re querying all posts that are post-type attachment, rather than only the posts that are attached to the current post being looped through in $media_query.

    Here’s how you loop through $media_query:

    foreach ($media_query as $media_query) :
    

    (Note: bad form. Try something like foreach ( $media_query as $media ) : instead.)

    You need to pass the ID of the current post to your $attachments query, as post_parent. Something simple might be:

    $attachments = get_posts( array(
        'post_status' => 'inherit',
        'post_type' => 'attachment',
        'post_parent' => $media_query->ID
        'numberposts' => 0
    ) );
    
  2. I got it! The result will show all attachments in a specific term inside term’s archive page. Thanks Chip!

    <?php $queried_object = get_queried_object();
    $term_id = $queried_object->term_id; 
    
     global $wp_query;
    
     $original_query['tax_query'] = array(
    array(
    'taxonomy' => 't-arte',
    'terms' => $term_id,
    'field' => 'id',
    ),);
    
       $original_query = (array) $wp_query;
    
    $attach_query = array(
        'post_type'=> array( 'attachment' ),
        'post_status' => array( null ));
    $args = array_merge($original_query['query_vars'], $attach_query);
    
    $media_query = new WP_Query( $args )?>
    
    <?php  if($media_query->have_posts()) :
    while ($media_query->have_posts() ) : $media_query->the_post();
    if( $post->ID == $do_not_duplicate ) continue; ?>
    
    <div id="archivespage-media-item">   
    
    <div id="imagem">
    <?php echo wp_get_attachment_link($attachment->ID, 'bigger-thumb');?>
         </div>
    
    </div>
    
    <?php endwhile; else: ?>    
    
    //do stuff       
    
     </div>