Why does this code not display in alphabetical order?

I am trying to display artists in alphabetical order. They kind of do bu then some letters are added to the end. You can see list by going here and rolling over artists in the menu (E, F, O and T appear at the end):

function get_artist_list(){

$terms = get_top_taxonomies('artist','all');
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
//$term_list = [];
$art_list = '';
$count = 0;
foreach ( $terms as $term ){
    $first_letter = strtoupper($term->name[0]);
    $term_list[$first_letter][] = $term;

foreach ( $term_list as $key=>$value ) {
$count ++;
        	$art_list .=  '<div class="col-md-3 col-sm-3">
            	<div class="black-bk active-bk">'.$key.'</div>
                <div class="artist-links">';
 foreach ( $value as $term ) {
                                        	$art_list .=  '<a href="' . get_term_link( $term ) . '">' . $term->name . '</a>';

$art_list .= '</div>
if($count %4 == 0){
				$art_list .= '<div class="art-bord" style="clear:both;margin: 10px 15px 15px;"></div>';
} }
			return $art_list;

Are E, F, O and T the last ones you entered into the database?

Are you selecting them in order from the database query, as I can not see you doing it anywhere in the code posted?

You need to format your code correctly for the HTML to show up.

You can highlight your code, then use the </> button in the editor window, which will format it.

Or you can place three backticks ``` (top left key on US/UK keyboards) on a line before your code, and three on a line after your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.

plz format your code in proper manner for html and remove the extra spaces. you can use sort($terms); to
arrange values in alphabetical order.

Have you tried any of the PHP built in array functions to sort your array?

There are over a dozen different ways to sort the items!


Thank you everyone for the help. I discovered the code was using a second function which was organising the categories by ID rather than by slug. All fixed now.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.