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.
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
)
);
}
Per your comment (and hopefully an edit to the question) :
Formatted:
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 runget_term_children
, though that also does not take a parameter that will order the results.You can sort the result set though: