Sorting Attributes order when using get_the_terms

You will need to sort them yourself:

 $terms = get_the_terms( $post->ID, 'category');
 foreach ( $terms as $term ) {
  $newterms[$term->slug] = $term;
 }
 ksort($newterms);
 foreach ( $newterms as $term ) {
  echo "<li>" .$term->name. "</li>";
 }

Or, if you feel adventurous, the same with a filter:

function alpha_sort_terms($terms) {
  remove_filter('get_the_terms','alpha_sort_terms');
  foreach ( $terms as $term ) {
    $newterms[$term->slug] = $term;
  }
  ksort($newterms);
  return $newterms;
}
add_filter('get_the_terms','alpha_sort_terms');

$terms = get_the_terms( $post->ID, 'category');
foreach ( $terms as $term ) {
  echo "<li>" .$term->name. "</li>";
}

Leave a Comment