How to order get_term_children output by alphabetic order

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>";

Leave a Comment