How to get_term_children output in alphabetical order?

How do i get output of this in alphabetical order

    $termID = 5;
    $taxonomyName = 'area';
    $termchildren = get_term_children( $termID, $taxonomyName );

    echo '<ul>';
    foreach ($termchildren as $child) {
        $term = get_term_by( 'id', $child, $taxonomyName );
        echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '" rel="nofollow noreferrer noopener">' . $term->name . '</a></li>';
    echo '</ul>';

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

get_term_children() only outputs the term IDs, and you later get details for each term using get_term_by(). You can combine these queries in one using get_terms() with the child_of argument:

get_terms( $taxonomyName, array( 'child_of' => $termID ) );

By default this sorts by name. However, it is possible that the child_of argument undoes the sorting. In that case you can sort it again using usort(). See an example at this answer for a related problem.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply