Order foreach $term by the date its added

I am using $terms to display the $term->name of a custom taxonomy I created called position.

However the terms are currently displaying alphabetically and I need them to display in the order that they are selected/added in the admin panel.

Read More

Here is my code so far, I’ve simplified so that its easier to see the area that I’m looking change the order of.

<?php
$posts = get_field('team_members',12);
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
$terms = get_the_terms( $post->ID , 'position', array( 'orderby' => 'post_date', 'order' => 'DESC' ) ); ?>
    <article>   
        <?php
            $i = 1;
            foreach($terms as $term) {

            if($i > 1){ 
             echo ' / '.$term->name;
            } else { 
                echo $term->name;
            }   
            $i++;  
            } ?>        
    </article>
<?php endforeach; ?>
<?php wp_reset_postdata();?>
<?php endif; ?>

There can be multiple terms per post. If there is more than one term then a / is added to seperate the terms. It is here that I need them ordered in the way they are selected/added.

Related posts

1 comment

  1. Maybe sort the data after the array is created?

    function sortFunction( $a, $b ) {
        return strtotime($a["post_date"]) - strtotime($b["post_date"]);
    }
    usort($terms, "sortFunction");
    

Comments are closed.