Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
        ) 
    );
}
share|improve this question
    
Where are you trying to add the orderby? Shouldn't there be a foreach or other loop operator on that fifth line down? –  s_ha_dum Jul 2 '13 at 17:30
    
I did it like so: 'foreach ( $termchildren as $child ) { $term = get_term_by( 'id', $child, $taxonomy_name, array( 'orderby' => 'name', 'hide_empty' => 0) );` Not sure what you mean about another foreach –  Chris Jul 2 '13 at 18:22
    
^^ Please edit that into the question. –  s_ha_dum Jul 2 '13 at 18:45

1 Answer 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>";
share|improve this answer
    
I realized orderby wouldn't work, but I wasn't sure how else to do it. Sorry to be simple, but where do I insert the code you provide into my original code? –  Chris Jul 2 '13 at 19:40
    
This works, thank you. Would there be any reason why two of these drop-downs (one for location, one for topic) on the same page would conflict? The second one I add seems to become non-functional after the first use of either drop-down. –  Chris Jul 2 '13 at 20:36
    
I'd have to see everything in context but that sounds like a Javascript issue. Check the console and check for invalid HTML. –  s_ha_dum Jul 2 '13 at 20:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.