0

After seeking help and failing, I managed to write a query for Wordpress that does exactly what it's suppose to do, however it pulls the post that it's suppose to in twice. I've stared and stared at the markup and I can't see why it's doing it. Here's the query:

<?php 
$the_query = new WP_Query( array(
    'post_type' => 'product',
    'tax_query' => array(
        'taxonomy' => 'supplier-tax',
    ),
) );

while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<?php 
    $terms = get_the_terms( $post->ID, 'supplier-tax');
    foreach ( $terms as $term ) {
        $termID[] = $term->term_id;
    } 

    $my_query = new WP_Query( array(
        'post_type' => 'supplier',
        'tax_query' => array(
            'field' => 'slug',
            'terms' => '$termID',
        ),
    ) ); ?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p class="supplier">Supplied by <strong><?php the_title(); ?></strong></p>
        <img src="<?php the_field('logo'); ?>">
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

If anybody could help, or point out where I'm going wrong so that i can learn from it, I'd really appreciate it.

I've been told that it's because the loop is running twice, but i'm not sure how tpo fix that.

  • WP_Query tax_query – Pieter Goosen Aug 6 '15 at 11:07
  • Could you help? – Addzy Aug 6 '15 at 11:40
  • Ok, I have deleted my answer. Now everything make sense, feel free to search my profile for related posts, I have done a few with your basic requirements. :-) – Pieter Goosen Aug 7 '15 at 11:13
  • Your link says "0 posts", thank you for your help. Should I leave my anser with mine then for now? – Addzy Aug 7 '15 at 11:22
  • Ok, don't know how that happened, but just then type "related posts" in the search box and hit search. You will get a lot of results which you can play with. As for your answer, it depends on you – Pieter Goosen Aug 7 '15 at 11:43
0

I solved it:

Just in case anybody ever has any trouble querying a post with the same term of a Taxonomy in another Post Type. I'll post my answer below because I finally solved it on my own.

<?php 

$terms = get_the_terms( $post->ID, 'supplier-tax');
foreach ( $terms as $term ) {
    $termID[] = $term->term_id;
}

$the_query = new WP_Query( array(
 'post_type' => 'supplier',
 'tax_query' => array(
     array(        
        'taxonomy' => 'supplier-tax',
        'terms' => $termID,
      )
    ),
) );

while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

   <p class="supplier">Supplied by <strong><?php the_title(); ?></strong></p>
   <img src="<?php the_field('logo'); ?>">

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

I hope this helps someone because this was hard to find a solution too.

  • tax_query should be an array of an array – Pieter Goosen Aug 6 '15 at 16:21
  • I don't understand, It works? what should i change it too? – Addzy Aug 6 '15 at 16:22
  • I have updated your answer to be correct – Pieter Goosen Aug 6 '15 at 16:33
  • Thank you very much, is there any chance you can explain what your change did and why it needed doing? just so I understand? – Addzy Aug 6 '15 at 16:35

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.