1

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();?>
  • What have you done to debug? What have you done to track down the source of the issue? Are you getting duplicate attachments, or duplicate posts with attachments? – Chip Bennett Apr 7 '14 at 16:41
  • Hi Chip! Sorry, I dont understand what do you mean as debug. I'm getting duplicate attachments. I wanna show the attachments with the same term. The results of attachments are showing 5 times. You can see a test here in topic "Imagens Relacionadas » Sakimi Chan" colorbits.com.br/t-arte/sakimi-chan – Bibiana Apr 7 '14 at 17:56
  • Looking at the output, I think you're getting duplicate posts, not duplicate attachments. More specifically, it looks like you're getting the same post, 5 times. For debugging, I would recommend verifying your query return values at each point that you loop through them. Start here: $media_query = get_posts($args); var_dump( $media_query ). Then try here: $attachments = get_posts( $args ); var_dump( $attachments ). – Chip Bennett Apr 7 '14 at 18:04
  • Related question: why are you passing the same arguments array to $media_query and $attachments? Don't you need to query children for $attachments? – Chip Bennett Apr 7 '14 at 18:05
  • I didnt found another way to show attachments :( – Bibiana Apr 7 '14 at 18:06
0

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
) );
  • I've tried it, but now its showing some random images that aren't related to the taxonomy term '$term_id' :/ $media_query = get_posts($args); if( !empty( $media_query ) ) : foreach ( $media_query as $media ) : global $post; $post = $media_query; setup_postdata($media_query); And attachments query as you shown. – Bibiana Apr 7 '14 at 19:25
  • My code was just example code. You'll need to adapt it as necessary (such as adding your tax_query). – Chip Bennett Apr 7 '14 at 19:35
  • If you could take a look at these: wordpress.org/support/topic/… It works fine! But has an error right behind the attachments that I want: Fatal error: Call to a member function have_posts() on a non-object in... The result here: colorbits.com.br/t-arte/sakimi-chan – Bibiana Apr 7 '14 at 19:37
  • 1
    Chip! I got it! :D I made something like these! wordpress.stackexchange.com/questions/29635/… Thanks a lot! :) – Bibiana Apr 7 '14 at 20:21
0

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>

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.