Ryan Brackett
Ryan Brackett

Reputation: 1022

Display ACF Taxonomy in order of Multi Select order

Using ACF, I have a Taxonomy field with Multi Select and Stylized UI.

I usually have 2 or 3 tax items selected.

I can arrange them using drag and drop. It's wonderful.

On the front end, the array does not reflect the tax order/sorting that I put in place.

Pretty easy code. What am I doing wrong?

                        <?php

                        $values = get_the_terms( $post->ID, 'languages' );

                        if ( $values ) {
                            echo '<tr class="item"><td>';

                                        foreach ( $values as $value ) {
                                            echo $value->name . '<br/>';
                                        }

                            echo '</td></tr>';
                        }
                        ?>

Upvotes: 0

Views: 3323

Answers (2)

Nebula Design
Nebula Design

Reputation: 11

I know it's an old thread but for those looking for the ACF-version:

<?php $terms = get_terms( array(
    'taxonomy' => '[YOUR TAXONOMY HERE]',
    'hide_empty' => false,
    'include' => $ids,
    'orderby' => 'include', // Order by your include
) );
//Rest of your code
?>

Upvotes: 1

Ryan Brackett
Ryan Brackett

Reputation: 1022

Figured it out. Because the field saved the correct sorting in the admin field, I knew the order data was saved somewhere. I looked in the database at the post's terms and the associated data. The order was saved there. Instead of using get_the_terms for the loop, I used get_post_meta. That gave me an array with the term IDs in the correct order. Then I got the term name using that ID within a foreach loop. This gave me the taxonomy names in the order from the styled multi-select field.

                        <?php

                        $values = get_the_terms( $post->ID, 'languages' );
                        if ( $values ) {
                            echo '<tr class="item"><td>';

                            $values = get_post_meta( $post->ID, 'languages' );

                            foreach ( $values as $value ) {
                                foreach ( $value as $item ) {
                                    $term = get_term( $item )->name; 
                                    echo $term .'<br/>';    
                                }
                            }

                            echo '</td></tr>';

                        }
                        ?>

Upvotes: 1

Related Questions