• I have a single custom post type of ‘products’ while that cpt is organized by 5 taxonomy categories which include ‘equipment’, ‘tools’ and ‘supplies’.

    On my single-equipment.php page (single custom post type template page) I would like to show ‘related-items’ where the page pulls other items from the related taxonomy shown on the given page. In a sense, it’s basically a “see more items like this one” type of thing.

    So let’s say a viewer is looking at a page for ‘window cleaner fluid’ that belongs to the category/taxonomy of ‘supplies’, I want to have 3 or 4 items pull from the ‘supplies’ taxonomy to show at the bottom of the post before the footer.

    So far, I have the following:

    function smt_related_items() {
    
      $terms = get_the_terms( get_the_ID());
      $args = array(
        'posts_per_page' => 4,
        'post__not_in' => array( get_the_ID() ),
        'category_name' => $terms[0]->slug
      );
      $loop = new WP_Query( $args );
    
      if( $loop->have_posts() ):
        echo '<div class="related-items"><h3>Related Items from Summit</h3>';
        while( $loop->have_posts() ): $loop->the_post();
          $classes = 'one-half relate-box';
          if( 0 == $loop->current_post || 0 == $loop->current_post % 2 )
            $classes .= ' first';
          echo '<div class="' . $classes . '"><a href="' . get_permalink() . '">' . get_the_post_thumbnail( get_the_ID(), 'pdt-thumb' ) . '</a><p>' . get_the_excerpt() .'</p></div>';
        endwhile;
        echo '</div>';
      endif;
      wp_reset_postdata();
    
    }

    but my page simply seems to only return 4 posts from wherever on the site, including from the normal posts categories with no distinguishing on where it’s pulling posts from. I’ve been stuck on this for like 3 days, and I can’t figure it out.

    Can anyone help!?

The topic ‘Taxonomy Query’ is closed to new replies.