Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a list of events on a clients website that show in a sidebar. When viewing the homepage you see one event and if you're anywhere else on the site you see two events in the sidebar. I am currently ordering them by using a meta_query compare that checks if the event is today or in the future.

What I would like to do is have the events show by closest to the current date first, then proceed up the chain until it finds one. Currently it appears to show events by furthest into the future instead. So if I have an event January 27th, 2012 and an event August 15th, 2012 it seems to show the August event.

The code I currently have is the following. I'd appreciate some advice on how to tweak it to get it to work the way I want. Please provide a little explanation with any code you post if possible, I'd like this to be a learning experience.

$totalPosts = 2;

    $args = array(
        'post_type'        => 'upcoming_courses',
        'order'            => 'ASC',
        'orderby'          => 'meta_value',
        'suppress_filters' => true,
        'posts_per_page'   => $totalPosts,
        'numberposts'      => $totalPosts,
        'meta_query' => array(
          array(
            'key' => 'course_date',
            'value' => date('Y-m-d'),
            'compare' => '>=',
            'type' => 'NUMERIC'
          )
        )
    );

    $my_query  = new WP_Query( $args );
share|improve this question

1 Answer 1

up vote 1 down vote accepted

This is just a shot in the dark but I would try ordering the events in descending order instead of ascending. 'order' => 'DESC'

share|improve this answer
    
Wow, just wow. I am an idiot, this was exactly what was needed. I was too engrossed in getting this site done that I got tunnel vision and didn't actually realise it's ordering them incorrectly. Thank you and welcome to the site Nickiler. –  DigitalSea Jan 18 '12 at 2:17
    
No problem, Dwayne. That's the nature of this business. Some things are simple to fix but not always obvious at first glance. Thanks for the welcome glad to be here. –  Nickiler Jan 19 '12 at 11:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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