Get Post from another post type that shares the same taxonomy?

I’ve got Two custom Post Types set up in WordPress, One being called Products and the other Suppliers. Slugs for these are 'product' and one being 'supplier'.

These two post types share a custom taxonomy called Suppliers which slug is 'supplier-tax'.This then has lots of children which are the different suppliers.

Read More

Basically, What I am trying to do is when you are on a single post page for a product, I need to pull in a post for the supplier as well. I thought that the best way to do this with how I have set it up is to select the appropiate supplier from the supplier taxonomy when on the product page. And this then queries and gets post from the ‘Supplier’ Post ttype with the same selected Taxonomy.

I wrote this query which brings in posts from the taxonomy, however It needs me to tell it which taxonomy and which slug etc to bring in plus it doesnt query the different post type which makes it useless, however it was a start:

<?php 
$the_query = new WP_Query( array(
    'post_type' => 'product',
    'tax_query' => array(
        'taxonomy' => 'supplier-tax',
        'field' => 'slug',
        'terms' => 'supplier_1',

    ),
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post(); ?>
        <?php the_title(); ?>
        <?php the_post_thumbnail('full'); ?>
        <?php the_content(); ?>

<?php endwhile; ?>


<?php wp_reset_postdata(); ?>

I’ve tried to adapt and include parts from queries I’ve fond on previous sources but I can’t seem to crack it. This is my attempt at it:

<?php  
    $terms = wp_get_post_terms( $post->ID, 'supplier-tax' );
    if($terms){
        $supplier_terms = array();
        foreach ($terms as $term){
        $supplier_terms[] = $term->slug;
    }

    $original_query = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query( array(
        'post_type' => 'supplier',
        'tax_query' => array(
            array(
            'taxonomy' => 'supplier-tax',
            'field' => 'slug',
            'terms' => $supplier_terms, //the taxonomy terms I'd like to dynamically query
            'posts_per_page' => '-1'
            ),
        ),
        'orderby' => 'title',
        'order' => 'ASC'
    ) );

    if ( have_posts() ): ?>
        <?php while (have_posts() ) : the_post(); ?>
        <?php the_title(); ?>"><?php the_title(); ?>
    <?php endwhile; ?>
    <?php endif;
        $wp_query = null;
        $wp_query = $original_query;
        wp_reset_postdata(); 
    } 
?>

Has anyone got any ideas on what I’m doing wrong or how I can make this work?

Related posts

1 comment

  1. I managed to find a solution to my problem, whilst I don’t think it is the cleanest markup, it works and does the job.

    <?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;
        }
        echo $termID[0]; 
    ?>
    
    
    <?php 
    $my_query2 = new WP_Query( array(
        'post_type' => 'supplier',
        'tax_query' => array(
            'field' => 'slug',
            'terms' => '$termID',
    
        ),
    ) ); ?>
    
        <?php while ($my_query2->have_posts()) : $my_query2->the_post(); ?>
            <?php the_title(); ?>
            <?php the_post_thumbnail('full'); ?>
            <?php the_content(); ?>
    
    <?php endwhile; ?>
    

Comments are closed.