Search code examples
phpwordpresssortingtaxonomy

How can I sort id's Alphabetically from get_term_children?


My code is as follows, but the list returned is not sorted alphabetically.

Any help?

<?php
    $termID = 4;
    $taxonomyName = "location";
    $termchildren = get_term_children( $termID, $taxonomyName );

    echo '<ul class="list">';
        foreach ($termchildren as $child) {
        $term = get_term_by( 'id', $child, $taxonomyName );
        echo '<li><span class="feature"><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' .   $term->name . '</a></li>';
        }
    echo '</ul>';
?> 

Solution

  • Use this :

             $termID = 4;
            $taxonomyName = "location";
            $termchildren = get_term_children( $termID, $taxonomyName );
    
                foreach ($termchildren as $child) {
                $term = get_term_by( 'id', $child, $taxonomyName );
    
                $namearray[$term->name]= get_term_link( $term->name, $taxonomyName );
    
                }
    
            print_r($namearray);
    
            ksort($namearray);
    
             echo '<ul class="list">';
                foreach ($namearray as $key => $value) {
                echo '<li><span class="feature"><a href="'.$value.'">' .   $key . '</a></li>';
                }
            echo '</ul>';