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 written an wp query which I would like to order in a bit of a complicated way. Some posts have a meta value 'the_date' which is an end date for the post. I would like to order posts in date and time order where if there is an end date this would be the reference how ever is there is no end date the publish date would be the reference. With this information I need to order posts by the closest to the current date and time. For example:

posts:

ID          Name         Publish_date        Meta_end_date
1           Post 1       15/05/2013 10:00   
2           Post 2       15/05/2013 09:00    17/05/2013 14:20
3           Post 3       16/05/2013 13:00    20/05/2013 08:00
4           Post 4       17/05/2013 13:50

current time: 17/05/2013 14:00

Result order:

Post 4 (17/05/2013 13:50), 
Post 2 (17/05/2013 14:20), 
Post 1 (15/05/2013 10:00),
Post 3 (20/05/2013 08:00)

Here is my current query:

$args = array(
    'post_type' => 'post',
    'numberposts' => '30',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'the_date',
            'value' => $today,
            'compare' => '>',
            'type'=> 'DATETIME'
         ),
        array(
            'key' => 'the_date',
            'value' => '',
            'compare' => '=',
        )
    ),
    'orderby' => 'post_date',
    'order' => 'ASC',
    'orderby' => 'meta_value_num',
    'order' => 'DESC'
);

Any help is very welcome :)

share|improve this question
    
defining array keys twice isn't going to work, it's actually invalid PHP code, if your code works but gives the wrong result, then you have performed miracles, it should give a syntax error –  Tom J Nowell May 17 '13 at 13:28
    
There is no syntax error, the above query works as expected, I just want to edit it to organize the results as explained. –  Paul Winter May 17 '13 at 13:37

1 Answer 1

try this, since you can pass multiple orderby values sparated by space.

$args = array(
    'post_type' => 'post',
    'numberposts' => '30',
    'orderby' => 'post_date the_date',
    'order' => 'DESC'
);

http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

share|improve this answer
    
Thank you for your feedback. Altho I'm sure that query will work, I don't think it will return past and future dates closest to the current time. –  Paul Winter May 17 '13 at 13:39
    
Because of the way that MySQL handles ORDER BY values, I don't think this is going to give the order the OP wants, and to get even close it would have to be 'the_date post_date '. the_date is supposed to have priority. With that change, it might work but I'd have to do some testing to be sure. –  s_ha_dum May 17 '13 at 13:40
    
Honestly, I don't even know if it can be done. It would need to be something link order by post_date ASC, order by the_date DESC and then some way of displaying them each closest to todays date. (Might have to sort the closest in PHP?) –  Paul Winter May 17 '13 at 13:55
    
I am sure it can be done but if this doesn't do it you will need to create some query filters. This comment makes the problem sound even more complicated than it seems in your question. Have you left something out? –  s_ha_dum May 17 '13 at 15:52

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.