1

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 :)

2
  • 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, 2013 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. May 17, 2013 at 13:37

1 Answer 1

2

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

4
  • 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. May 17, 2013 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, 2013 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?) May 17, 2013 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, 2013 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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