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.

6
  • what do you mean by however it pulls the post that it's suppose to in twice.? Commented Aug 6, 2015 at 9:54
  • At the moment theres only one post connected with term of the taxonomy it's getting, and when it pulls that post into the page, it does it twice, it gives the content twice. Essentially putting two of the same post on a page Commented Aug 6, 2015 at 9:56
  • it is because the loop run twice. what am i understand is : that you want post from custom post type supplier where supplier-tax taxonomy linked with ? is it ? Commented Aug 6, 2015 at 9:59
  • Yes essentially, I have two custom post types and a post in each post type will share a term in the supplier-tax taxonomy. I need to pull the post that shares the same term into the other post's page. Commented Aug 6, 2015 at 10:02
  • you want post attached with category supplier-tax please confirm ? Commented Aug 6, 2015 at 10:05

2 Answers 2

1

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( 'taxonomy' => 'supplier-tax', 'field' => 'slug', '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.

Sign up to request clarification or add additional context in comments.

1 Comment

in variable $termID,you get taxonomy id and you pass that $termiD in query array filter by "SLUG"..! you can use 'field' =>'term_id'...
0

It will return all the post associated with taxonomy & post type your provide .

1st answer :

 $term_slug = get_query_var('term'); $args = array( 'post_type' => array('product','supplier'), 'tax_query' => array( 'taxonomy' => 'supplier-tax', 'field' => 'slug', 'terms' => $term_slug, ) ); $my_query = new WP_Query( $args ); ?> <?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; ?> 

?>

2nd answer :

$posts = get_adjacent_post( true, '', true, 'supplier-tax' ); echo '<pre>';print_r($posts);echo '</pre>'; 

12 Comments

Thanks for the attempt but, it pulls all the posts in from that taxonomy, I only need to pull the posts from the corresponding term
what is the corresponding term name ?
It changes on every page which is what the following code did $terms = get_the_terms( $post->ID, 'supplier-tax'); foreach ( $terms as $term ) { $termID[] = $term->term_id; } it got the term name, for you to create a variable and use that.
hey try this , is it working fine $posts = get_adjacent_post( true, '', true, 'supplier-tax' ); echo '<pre>';print_r($post);echo '</pre>'; just remove all code and try.
Can you edit your answer to show me where it's suppose to go
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.