How to order get_term_children output by alphabetic order

The following creates drop-down lists of my custom taxonomy children, but the order of the output seems to by creation date, not by alphabetical name. I tried adding orderby to the array with no success.

I’ve read about usort but am not sure how to implement it.

Read More

I’d appreciate any help.

$term_id = 61;
$taxonomy_name = 'tribe_events_cat';
$termchildren = get_term_children( $term_id, $taxonomy_name );

echo "<script type='text/javascript'>function taclinkch(x){var y=document.getElementById(x).value;document.location.href=y}</script>";
echo "<form action=''>  <select name='eventcats' id='eventcats'   onchange='taclinkch(this.id)'>";

echo "<option value=''>Select a Topic</option>";

foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );

    echo "<option value='" .  get_site_url() . "/events/category/". $term->slug." '>" . $term->name . "</option>";
}

echo "</select></form>";

I added this array, which didn’t work:

foreach ( $termchildren as $child ) { 
    $term = get_term_by( 
        'id', 
        $child, 
        $taxonomy_name, 
        array( 
            'orderby' => 'name',
            'hide_empty' => 0
        ) 
    );
}

Related posts

1 comment

  1. Per your comment (and hopefully an edit to the question) :

    I did it like so: foreach ( $termchildren as $child ) { $term =
    get_term_by( 'id', $child, $taxonomy_name, array( 'orderby' => 'name',
    'hide_empty' => 0) );

    Formatted:

    foreach ( $termchildren as $child ) { 
      $term = get_term_by( 
        'id', 
        $child, 
        $taxonomy_name, 
        array( 
          'orderby' => 'name',
          'hide_empty' => 0
        ) 
      );
    

    get_term_by does not accept an array as the fourth parameter, or in fact any parameter. You can’t just make up parameters and expect them to work. You need to look at the Codex and the source.

    Besides, get_term_by is not where the ordering happens. The ordering occurs when you run get_term_children, though that also does not take a parameter that will order the results.

    You can sort the result set though:

    $term_id = 61;
    $taxonomy_name = 'tribe_events_cat';
    $termchildren = get_term_children( $term_id, $taxonomy_name );
    $children = array();
    foreach ($termchildren as $child) {
      $term = get_term_by( 'id', $child, $taxonomy_name );
      $children[$term->name] = $term;
    }
    ksort($children);
    // now $children is alphabetized
    echo "<script type='text/javascript'>function taclinkch(x){var y=document.getElementById(x).value;document.location.href=y}</script>";
    echo "<form action=''>  <select name='eventcats' id='eventcats'   onchange='taclinkch(this.id)'>";
      echo "<option value=''>Select a Topic</option>";
      foreach ( $children as $child ) {
        $term = get_term_by( 'id', $child->term_id, $taxonomy_name );
        echo "<option value='" .  get_site_url() . "/events/category/". $term->slug." '>" . $term->name . "</option>";
      }
    echo "</select></form>";
    

Comments are closed.