I have a Custom Post Type 'my_cpt' and a custom taxonomy 'types' which has several terms, all of which have posts.

I want to do is be able to control both the order of the groups (terms from my custom taxonomy 'types') AND the order of the Items (posts from 'my_cpt' custom post type and the term of the group it is in) within each group by using menu_order in the admin area.

I have this code below (see far bottom) which gets me as far as this:

Taxonomy term 1

  • Item 1 from taxonomy term 1
  • Item 2 from taxonomy term 1

Taxonomy term 2

  • Item 1 from taxonomy term 2
  • Item 2 from taxonomy term 2
  • Item 3 from taxonomy term 2

Taxonomy term 3

  • Item 1 from taxonomy term 3

I can change the order of the groups (Taxonomy term 1, 2, 3 etc) by name if I change:

$custom_terms = get_terms('surface_bonding_types');


$custom_terms = get_terms('surface_bonding_types', 'orderby=name&order=DESC');

So I know the orderby is working for the groups. But I can't see a way to change the order of my Taxonomy terms as there is no 'Order' field as I have with Pages in the admin.

I am completely unable to change the order of the Items within each group. I thought that adding orderby and order parameters to the tax_query array would do the job but it doesn't do anything.

                    $custom_terms = get_terms('types');
                    foreach($custom_terms as $custom_term) {
                        $args = array('post_type' => 'my_cpt',
                            'tax_query' => array(
                                    'taxonomy' => 'types',
                                    'field' => 'slug',
                                    'terms' => $custom_term->slug,
                         $loop = new WP_Query($args);

                         if($loop->have_posts()) {
                            echo '<h3>'.$custom_term->name.'</h3>';
                                while($loop->have_posts()) : $loop->the_post();
                                        <a href="<?php echo get_permalink(); ?>">
                                            <?php echo the_post_thumbnail('thumbnail' ); ?>
                                            <?php echo get_the_title(); ?>                                              

I could what I want to achieve by running multiple wp_query on the page, one for each term but I would end up with 10 almost identical queries on the page. This code feels really close, I just can't quite get the last piece of logic. Any help greatly appreciated.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.