I'm trying to use this answer to order categories based on their Advanced Custom Fields custom field: track_order.

I'm able to get the UL to print fine and show the custom field numbers

custom categories order image

but I'm unable to pass the new sorted terms to the foreach loop for the individual categories.

    //$terms = get_terms("crb_issues");
    $terms = get_categories('taxonomy=tracks');
    $issue_archive = array( ); // creates an array for all terms inside of crb_issues tax using the custom field "issue_date" as the key
    foreach ( $terms as $term ) {
        $issue_date = get_field( 'track_order', $term );
        $issue_archive[$issue_date] = $term->name;

    krsort( $issue_archive, SORT_NUMERIC ); //sorts the issue_archive array from high to low

    foreach ( $issue_archive as $issue_date => $term_name ) {
        echo "<li>" . $term_name . " " . $issue_date . "</li>"; //displays the term name and customfield issue_date
        if (++$i == 4) break; //Stops the foreach after 4

    <?php foreach($terms as $term): ?>

Which gives me this order for my template layout:

not using order I want

How can I get the order being generated by the ACF custom field to be passed to the foreach loop?


2 Answers 2


There's no need to "pass" the order to your third foreach - just loop over $issue_archive again instead of $terms.

  • that didn't work, the other solution is working well, though. thx
    – tigre
    May 10, 2015 at 15:30

After more sleuthing, found an appropriate solution:

    // Solution for sorting by ACF custom-field for tracks categories
    // http://support.advancedcustomfields.com/forums/topic/sorting-categories-list-by-custom-field/
    $categories = get_categories('taxonomy=tracks');
    $sorted_cats = array();
    foreach($categories as $cat){
    //$ordr = get_field('track_order', 'tracks_'.$cat>term_id); //wasn't working so used the line below
        $ordr = get_field( 'track_order', $cat );
        $sorted_cats[$ordr] = $cat;
    krsort($sorted_cats);//ksort orders ascending, krsort reverses order (i.e. biggest to smallest)

    /* Test to print out order... not sure why cat name not showing...
    foreach ( $sorted_cats as $ordr => $term_name ) {
        echo "<li>" . $ordr->name . " " . $ordr . "</li>"; //displays the term name and customfield issue_date
        if (++$i == 4) break; //Stops the foreach after 4

I'd like to get the test <li> to show the category name, but I can't get it to work. All that prints is the ACF order number. Any help on that part is greatly appreciated.

Here is a screenshot showing how the categories are now ordering correctly (big to low with Test Track = 200):

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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