query – Sort query_terms_list for post_tags alphabetically

Question

I have the following array that currently sorts tags by tag count and returns the tags with the individual tag count. I would like to keep the individual tag count but sort the tags alphabetically instead. I tried to change arsort( $list ) to sort ( $terms->name ) but I think my logic here is completely off. Any assistance is much appreciated 🙂

Example:
change from: oranges(20 )apples(12) bananas(10)
to
apples(12) bananas(10) oranges(20)

function get_query_terms_list( $taxonomy = 'post_tag', $sep = '' ) {
$list = array();

foreach ( $GLOBALS['wp_query']->posts as $post ) {
    if ( is_array( $terms = get_the_terms( $post, $taxonomy ) ) ) {
        foreach ( $terms as $term ) {
            // Set the term's initial count.
            if ( ! isset( $list[ $term->term_id ] ) ) {
                $list[ $term->term_id ] = 0;
            }
            // And then increment it for each post.
            $list[ $term->term_id ]++;
        }
    }
}


// Sort by the count, highest to lowest.
arsort( $list );

0
RobH 4 months 2022-04-22T17:03:48-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse