1

I have a custom post type registered (our-team) and I have a taxonomy (team-categories) with multiple terms. Each term has multiple posts assigned to it. On my custom post type archive (archive-our-team.php) I am using the code below to separate my loop by term, which is working great, but defaults to alphabetical ordering by term:

A Term Name - all posts in this term

B Term Name - all posts in this term

C Term Name - all posts in this term

I can't figure out how to customize the order of the terms so I can get something like (any order I want):

C Term Name - all posts in this term

A Term Name - all posts in this term

B Term Name - all posts in this term

Is there a way to set an order number for each term in a taxonomy, so when I get_terms and then output with foreach they are in the order I want?

<?php //for a given post type, return all
$post_type = 'our-team';
$tax = 'team-categories';
$tax_terms = get_terms($tax);
if ($tax_terms) {
    foreach ($tax_terms  as $tax_term) {
        $args=array(
          'post_type' => $post_type,
          "$tax" => $tax_term->slug,
          'post_status' => 'publish',
          'posts_per_page' => -1,


        );

        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {

            echo '<h2 style="clear:both;">'.$tax_term->name.'</h2>';
            while ($my_query->have_posts()) : $my_query->the_post(); 
             //set vars for custom fields
            $team_location = get_field('team_location');
            $team_position = get_field('team_position');
            $team_email = get_field('team_email');
            $team_name = get_the_title();
            $team_link = get_permalink();

            //get only first name. usage: echo $first_name[0];
            $team_first_name = $team_name;
            $first_name = explode(' ', trim($team_first_name));
            ?>
                <div class="team_member">
                    <?php if ( has_post_thumbnail()) { ?>
                        <a class="thickbox" href="<?php echo $team_link; ?>"><?php the_post_thumbnail('medium'); ?></a>                     
                    <?php } else { ?>
                        <img src="<? bloginfo('template_directory' );?>/library/images/no_pic.jpg" alt="no image provide" />
                    <?php } ?>

                    <div class="team_member_info">
                        <h2><?php the_title();?></h2>
                        <p><a class="thickbox" href="<?php echo $team_link; ?>">More info <?php// echo $first_name[0]; ?></a></p>
                        <?php if ($team_email) : ?><p><a href="mailto:<?php echo $team_email;?>">Email <?php echo $first_name[0]; ?></a></p><?php endif; ?>
                    </div>
                </div> <?php 
            endwhile;
        } 
  }
} 

?>

2 Answers 2

1

You can use this awesome plugin to give a sort order for taxonomy. It's old but still work on WordPress 3.6 and probably the latest version too.

1
  • Thanks for the find @aurevilly. Great plugin indeed - simply worked. I'm hesitant to use 'old' plugins but what can I say - WordPress is awesome with backwards compatibility.
    – user27786
    Commented Sep 27, 2013 at 20:49
0

My solution was to store the term order as metadata for the post.

//reset terms
if( is_array( $_POST['genres'] ) && count( $_POST['genres'] ) ){
    $_POST['genres_order'] = ( $_POST['genres'] = array_map( 'intval', $_POST['genres'] ) );
    wp_set_object_terms( $_POST['ID'], $_POST['genres'], 'nv_genre', false ); //will replace any previous genres
}

Then when I retrieve the terms I can order them by id. More detailed explaination here: Control term order on a per-post basis

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.