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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.