Support

Account

Home Forums ACF PRO Ordering taxonomy terms

Solving

Ordering taxonomy terms

  • Hello,

    I have a custom post type with a taxonomy ‘departments’. I am using the basic foreach loop, but the terms are coming out in a random order, and I’d like to order them alphabetically, but I can’t figure out how to do it.

    Is there any way that I can order the terms that are coming out?

    Thanks!

  • I figured it out… for anyone that’s looking for the answer:

    <?php
    
    $terms = get_field('portfolio');
                     
                                     usort($terms, function($a, $b) {return strcmp($a->name, $b->name);});
                                     if( $terms ):
                                         echo '<ul>';
                     
                                         foreach( $terms as $term ):
                     
                                           echo '<li>';
                     
                                             $name = $term->name;
                     
                                             echo $name;
                     
                                           echo '</li>';
                  
                                         endforeach;
                                         echo '</ul>';
                                     endif;
    
    ?>
  • ACF returns them in the order that they are selected, which may be why they seem random.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Ordering taxonomy terms’ is closed to new replies.