Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get_tags() function not sorting properly

Tags:

php

wordpress

Despite lots of documentation and examples online, I'm can't seem to make this simple function work properly. I have no idea what I'm doing wrong, but nonetheless, this isn't working properly. Can anyone spot what I'm missing here?

I'm looking to make a custom Tag Cloud, which is why I'm not using wp_tag_cloud().

$tags = get_tags( array('orderby' => 'name', 'order' => 'ASC'));
    foreach($tags as $tag) {
        echo "<li><a href=\""
                    .get_tag_link($tag->term_id)."\">"
                    .ucwords($tag->name)
                    ."</a> ($tag->count related page)</li>";    
    }

This produces the following output:

 - Black Box (3 related page)
 - Waste (2 related page)
 - Recycling (2 related page) 
 - Garbage (1 related page) 
 - Cheese (1 related page)
 - Blue Box (1 related page) 
 - Test (1 related page)

As you can see, they're sorted by COUNT, and not name. I have no idea why. My arguments seem to be ok. thoughts?

like image 961
MetalAdam Avatar asked Jan 26 '26 09:01

MetalAdam


1 Answers

This question is old, but maybe this help someone still searching for answer.

function sortOrder($a, $b) {
    if($a->name == $b->name){ return 0 ; }
    return ($a->name < $b->name) ? -1 : 1;
}

$tags = get_tags();
usort($tags, 'sortOrder');

    foreach($tags as $tag) {
        echo "<li><a href=\""
                    .get_tag_link($tag->term_id)."\">"
                    .ucwords($tag->name)
                    ."</a> ($tag->count related page)</li>";    
    }
like image 144
Amino Avatar answered Jan 27 '26 23:01

Amino