Support

Account

Home Forums General Issues Sort Custom Post Type by Taxonomy.

Solving

Sort Custom Post Type by Taxonomy.

  • Hi
    I have a query I would like to resolve.

    The scenario is as follows:
    I have a custom post type called Auction.

    Auctions have an acf in relation to a custom post type product.
    Those products can be assigned taxonomies.

    My question is how to sort those products by taxonomy when I’m on the single-auction.php page.

    Following the documentation I can show all the products of an auction, but I would like to sort those products by their taxonomy.

    An image to clarify what I want to get :

    A brief explanation:

    Taxonomy Term A

    Product 1
    Product 2

    Taxonomy Term B

    Product 3
    Product 4
    Order custom post type by taxonomy

    My code :

    $featured_posts = get_field(‘products_in_this_auction’);
    (Note: get_field(‘products_in_this_auction’) is a Post Object)
    This is a post object and contains the ids of the products that are linked to the auction.

    But my problem is how to get those products sorted by taxonomy.
    I guess I have to use tax_query for my
    query.

    I have an old code that shows me all the custom post type products ordered by their taxonomy, as seen in the image, but what I need is to show only the custom post type products that depend on the current auction.

    I know those custom post type are in $featured_posts = get_field(‘products_in_this_auction’); but I don’t know how to get them.

    My code, something old-fashioned, is the following.

     $categories = get_terms('type_produc', 'orderby=count&order=DESC&hide_empty=1');
                          foreach( $categories as $category ): 
                          ?>
                          <h3 class="title_pru"><?php echo $category->name; // Prints the cat/taxonomy group title ?></h3>
                          <?php
                          $posts = get_posts(array(
                          'post_type' => 'myproduct',
                          'taxonomy' => $category->taxonomy,
                          'term' => $category->slug,
                          'nopaging' => true,
                          ));
                          foreach($posts 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; ?>

    This code shows the result of the image, but it doesn’t work for me.

    I hope someone can help.
    Thank you very much.

  • You need to add a posts__in argument that uses the post object field value.

    
    // get the unformatted value of the field by setting
    // third argument to false, returns array of posts IDs
    $products = get_field('products_in_this_auction', false false);
    
    $posts = get_posts(array(
    'post__in' => $products,
    'post_type' => 'myproduct',
    'taxonomy' => $category->taxonomy,
    'term' => $category->slug,
    'nopaging' => true,
    ));
    
  • 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; ?>
  • That is why I gave you code. This code should be used where you are getting your posts. It limits the posts shown to only those selected by the post object field.

  • hi,
    I’m sorry my friend, I don’t understand you.
    Sorry I’m not that good with WordPress.

    Should I manipulate the get_terms function with one of its many parameters?
    I tried to follow you but it didn’t work.

       $categories = get_terms( 'type_produc', array(
                            'orderby' =>   'count',
                            'order'=>'DESC',                        
                            'hide_empty' => true,
                            'fields'     => 'ids' // (or parent) but not found 
                        ) );

    What you are telling me is that first I must save the posts that produce the code you gave me and then look for the taxonomy of each one?

    Sorry I didn’t understand.
    Thanks for your help and sorry for not understanding.

  • 
    $products = get_field('products_in_this_auction', false false);
    $categories = get_terms('type_produc', 'orderby=count&order=DESC&hide_empty=1');
                          foreach( $categories as $category ): 
                          ?>
                          <h3 class="title_pru"><?php echo $category->name; // Prints the cat/taxonomy group title ?></h3>
                          <?php
                          $posts = get_posts(array(
    'post__in' => $products,
                          'post_type' => 'myproduct',
                          'taxonomy' => $category->taxonomy,
                          'term' => $category->slug,
                          'nopaging' => true,
                          ));
                          foreach($posts 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; ?>
    
  • Muchas gracias por tu trabajo.
    Ha el problema ha quedado resuelto.
    Ya convenci al admin para que utilize categorias diferentes en cada subasta.

    Aunque estas categorias tengan el mismo nombre, si cambiamos el slug funcionara correctamente.

    Ya que despues de todo el trabajo que me continuaba mostrando categorias que tenian post de otras subastas. Eso se produce, supongo, por que la funcion get_terms me trae todos los terminos que no estan vacios, sin considerar a que subasta pertenecen.

    SALUDOS y muchas gracias por tu trabajo.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.