• Hi all,

    Within a custom post type and a custom taxonomy I have a list of posts, and if those posts are attributed to a custom taxonomy then they are bubbled up in to an accordion style nested list.

    The goal is to have the following type of list

    Product A
    Product B
    Product Tax
    Product C
    Product D
    Product E
    Product Tax 2
    Product F

    I have created two loops and it’s very close, but I can only list the products, and then the taxonomies with the products. I can’t find a way to bubble them all up in to one array and spit them out alphabetically.

    Here is my loop:

    <?php 
      //loop1
      $query1 = new WP_Query( array( 'post_type' => 'product_info', 'order' => 'ASC', 'orderby' => 'title', 
      'tax_query' => array(
            array(
                'taxonomy'  => 'need_cat',
                'terms' => $term_ids,
                'field' => 'term_id',
                'operator'  => 'NOT IN'
              )
          ),
      ));
    
      //loop2
      foreach($terms as $custom_term) {
          wp_reset_query();
          $args = array('post_type' => 'product_info',
              'tax_query' => array(
                  array(
                      'taxonomy' => 'need_cat',
                      'field' => 'slug',
                      'terms' => $custom_term->slug,
                  ),
              ),
           );
    
           $loop = new WP_Query($args);
           if($loop->have_posts()) {
              echo '<li class="productCat toggle">'.$custom_term->name;
              echo '<ul>';
              while($loop->have_posts()) : $loop->the_post();
                  echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
              endwhile;
              echo '</ul></li>';
           }
      }
    
      /* Restore original Post Data */
      wp_reset_postdata();
    ?>

    after this i then just do a simple loop for my $query1 posts.
    any help is really appreciated

  • The topic ‘Two Loops, How to combine them?’ is closed to new replies.