wp_get_object_terms – How can I order the resulting array by name or slug and hierarchy?

Question

I’m returning a custom taxonomy via wp_get_object_terms.

The trouble is that the results are not order by reliable.

I’m using a custom taxonomy for “Location” – Country, State, City – sometimes the State appears in other Country

$my_query = new WP_Query( array(
                'post_type'=>'listing',
                'posts_per_page'=>-1,
                'tax_query'=>array(
                    array(
                        'taxonomy'=>'listing-category',
                        'field'=>'id',
                        'orderby' => 'slug',
                        'order' => 'DESC',
                        'hierarchical' => true,
                        'terms'=>'478'
                    )
                 )
            ) );
            $unique_terms = array(); 
            $unique_terms_state = array(); 
            $unique_terms_city = array(); 
            while ( $my_query->have_posts() ) {
                    $my_query->the_post();
                    $post_id = get_the_ID();


                    $term_list = wp_get_object_terms( $post_id, 'location', array('parent' => 0, 'orderby' => 'slug', 'order' => 'ASC' ) );
                    foreach( $term_list as $term ) :
                        if( !in_array( $term->term_id, $unique_terms ) ):
                            array_push( $unique_terms, $term->term_id );
                            echo $term->name . '<br/>';

                        endif;  

                            $term_list_state = wp_get_object_terms( $post_id, 'location', array('parent' => $term->term_id, 'orderby' => 'slug', 'order' => 'ASC' ) );
                            foreach( $term_list_state as $term_state ) :
                                if( ! in_array( $term_state->term_id, $unique_terms_state ) ):
                                array_push( $unique_terms_state, $term_state->term_id );
                                    echo ' - ' . $term_state->name . '<br/>';
                                endif;  


                                    $term_list_city = wp_get_object_terms( $post_id, 'location', array('parent' => $term_state->term_id, 'orderby' => 'slug', 'order' => 'ASC' ) );
                                    foreach( $term_list_city as $term_city ) :
                                        if( ! in_array( $term_city->term_id, $unique_terms_city ) ):
                                        array_push( $unique_terms_city, $term_city->term_id );
                                            echo ' -- ' . $term_city->name . '<br/>';
                                        endif;  

                                    endforeach; 

                            endforeach; 

                    endforeach; 

                }

Is there a way I can order the resulting array by the custom taxonomy hierarchy?

Is there perhaps, another way I should be looking at this?

0
, user2241199 8 months 0 Answers 53 views 0

Leave an answer