Long story short, I inherited a Wordpress site at work and I have an events page that I'm working on/fixing. I'm new to both Wordpress and php and I could use some help.

Each event is categorized by school semester (Spring, Summer, Fall). However, I cannot get the semesters to order anything other than alphabetically (it's currently listed as: Fall, Spring, Summer...but I need: Spring, Summer, Fall).

I'd like to order the semesters by slug since I have the slugs set as semester code numbers that are in the correct order (Spring = 14, Summer = 15, Fall = 16).

Here's my code:

 public function eventCalendar() {
    global $post;
    $type = array('event');
    $terms = get_terms('semesters');
    $markup = '';
    $count = 0;

    // List the semester name, 
    // and list each event under that term.
    // Events should be listed chronologically based on start date.
    $markup .= '<ul class="event-calendar">';
    foreach ($terms as $semester) :
      $markup .= '<li class="event-calendar__semester">';
      $markup .= '<h4>' . $semester->name . '</h4>';

      $events = new WP_Query(array(
        'post_type' => 'events',
        'posts_per_page' => -1,
        'meta_key' => 'start_date',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        'tax_query' => array(
            'taxonomy' => 'semesters',
            'terms'    => $semester->term_id,


      if ($events->have_posts()) :
        $markup .= '<div class="event-calendar__events">';

        while ($events->have_posts()) : $events->the_post();
          $sd = get_field('start_date', false, false);
          $ed = get_field('end_date', false, false);
          $startdate = new DateTime($sd);
          $enddate = new DateTime($ed);

          $markup .= '<div class="event-calendar__event">';

            $markup .= '<span class="event-date__start">' . $startdate->format('M d') . '</span>';
            $markup .= (($ed) && $sd !== $ed) ? ' &ndash; <span class="event-date__end">' . $enddate->format('M d') . '</span>' : '';
            $markup .= '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';

          $markup .= '</div>';

        $markup .= '</div>';


      $markup .= '</li>';

    $markup .= '</ul>';

    return $markup;

I've tried several different combinations of 'order' and 'orderby' to no avail. Any help would be seriously appreciated by this newb!

  • It looks to me like your code is trying to order the events rather than setting any order for the semesters. In line 4 where you get_terms('semesters') you need to change the order of $terms by ascending order of term ID. – WebElaine Jun 8 '17 at 20:29

If I understand your scenario, your events are in the right order but your semesters are not.

If you change line 4

$terms = get_terms('semesters');


$terms = get_terms('semesters', array(
    'orderby' => 'slug'
) );

that should put the semesters in the order you're looking for. 'order' defaults to ASC, which is what you need to display 14, then 15, then 16, so you shouldn't need any other parameters.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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