Context

  • Post Type: Resources
  • Taxonomy: Media Type, Term: Audio
  • Taxonomy: Series

The following code displays a unique list of the custom taxonomy “Series”

I want to order the list by term_order, but it is not working. Any suggestions?

The site. Currently it’s ordered by ID

    <?php
                $post_data = array();

                $my_query = new WP_Query( array( 
                        'post_type' => 'resource',
                        'posts_per_page' => -1,
                        'media_type' => 'audio'

                    ) 
                );

                if ( $my_query->have_posts() ) {
                    while ( $my_query->have_posts() ) {
                        $my_query->the_post();
                        $post_data[] = get_the_ID();
                    }
                }

// Start with whatever ID you want to have from Media Type
                $audio = 16;

                // Get all those post_ids for that term
                $post_ids = get_objects_in_term( $audio, 'media_type', array('post_status' => 'publish') );

                // Then get the series terms for those post ids
                $terms = wp_get_object_terms( $post_data, 'series', array('fields' => 'ids', 'orderby' => 'menu_order' ) );
                $result = array_values( array_unique($terms) );

                ?>


            <?php
        $a = $result;

        foreach ($a as $v) {
        $term = get_term( $v, 'series' );
        $name = $term->name;
        $slug = $term->slug;
        echo '<div class="resource-item"><a href="'.get_term_link($slug, 'series').'" title="'.$name.'"><div class="play"></div><li>'.$name.'</li></a></div>';      
        } 

        ?>

6 s
6

As this is one of the top results on Google and none of the above quite worked for me, this did seem to return terms in an order matching their display in the admin…

 get_terms([
  'taxonomy'   => 'whatever_you_want',
  'meta_key'   => 'order',
  'orderby'    => 'meta_value_num'
]);

In case it helps anyone, WP stores a value in the termmeta table under the key order which represents its display position in the admin menus. Obviously it would need to be adapted if you wanted to query on other meta at the same time.

Leave a Reply

Your email address will not be published. Required fields are marked *