Support

Account

Home Forums General Issues Sort Custom Post Type by Taxonomy. Reply To: Sort Custom Post Type by Taxonomy.

  • Hi,
    Thank you very much for your help.
    I have been able to solve a large part of the problem.
    Thank you very much for responding so promptly.

    Order Custom Post Type by Taxonomy-OK

    The only problem with this solution is that WordPress shows me Taxonomies that have posts from other auctions.

    While WordPress does not show empty taxonomies thanks to the ‘hide_empty’ rule hide_empty => true , it does show taxonomies that have a custom post type but belong to another auction.

    Do you have any idea what I should change in the code below so that it only shows the taxonomies that are associated with the products of this auction?

    Greetings and thank you very much for your help.

    My code:

         $products = get_field('products_in_this_auction', false, false);      
                          $categories = get_terms( 'type_produc', array(
                            'orderby' =>   'count',
                            'order'=>'DESC',                        
                            'hide_empty' => true
                        ) );
                         
                         
                          foreach( $categories as $category ): 
                           
                          ?>
                          <h3 class="title_pru"><?php echo  $category->name;  ?></h3>
                          <?php
                        $query = get_posts(array(
                            'post__in' => $products,
                            'post_type' => 'myproduct',
                            'taxonomy' => $category->taxonomy,
                            'term' => $category->slug,
                            'nopaging' => true,
                            ));
                          foreach($query as $post): 
                          setup_postdata($post); 
                          ?>
                         
                          <div id="post-<?php the_ID(); ?>">
                         
                        
                        <?php  the_title( sprintf( '<p class="title_pru"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></p>' ); ?>
                         
                       
                         
                          </div><!-- #post-## --> 
                          <?php endforeach; ?>
                         
                         <?php endforeach; ?>