Home Forums Feature Requests Sort Taxonomy terms Reply To: Sort Taxonomy terms

  • I don’t think there is any automatic way to do this. The way I do it is that I add a field to the term editor page, usually called something like “sort_order”.
    Then my code for sorting them will look something like this:

    $taxonomy = 'my_taxonomy';
    $args = array(); // whatever arguments you're using
    $terms = get_terms($taxonomy, $args);
    $count = count($terms);
    for ($i=0; $i<$count; $i++) {
      $terms[$i]->sort_order = get_field('sort_order', $taxonomy.'_'.$terms[$i]->term_id;
    usort($terms, 'my_sort_terms_function';

    the sorting function looks something like this:

    function my_sort_terms_function($a, $b) {
      // this function expects that items to be sorted are objects and
      // that the property to sort by is $object->sort_order
      if ($a->sort_order == $b->sort_order) {
        return 0;
      } elseif ($a->sort_order < $b->sort_order) {
        return -1;
      } else {
        return 1;