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.

Below is the code I'm using to query multi-day events. The problem I'm having is that I can either sort by the start date (evstart_date) OR I can do a combined query against start date and end date (evend_date). But I cannot do both the combined query AND sort. The code below gets the query right (continues to display multi-day events that started in the past but have not yet ended), but the sort is broken (reverts to default sort by post date).

I've tried several variations, but have been unable to find a solution.

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $today = date('Y-m-d', strtotime('-6 hours'));
    $upcomingevents = new WP_Query(array(
        'post_type' => 'events', 
        'posts_per_page' => 6,  
        'paged' => $paged,
        'meta_query'=>array(
        'relation'=>'OR',
            array(
                'key' => 'evstart_date',
                'value' => $today,
                'compare' => '>=',
                'type' => 'CHAR'
                ),
            array(
                'key' => 'evend_date',
                'value' => $today,
                'compare' => '>=',
                'type' => 'CHAR'
                )
             ),
        'meta_key' => 'evstart_date',
        'orderby' => 'meta_value',
        'order' => 'ASC'
    )); 
?>
share|improve this question
1  
What do you want to sort by? –  tollmanz Feb 7 '12 at 5:36
    
What is the format of your dates? –  m0r7if3r Feb 7 '12 at 10:25
    
I want items ordered by start date. Dates are in yyyy-mm-dd format. –  Ray Gulick Feb 7 '12 at 14:01
    
Actually, above code doesn't get the query right, either, as it shows everything in events (added some events that were past and it does not exclude them). Again, if I remove the orderby code (last 3 variables) the query works (excludes appropriate items). If I query only for start date or end date, then I can sort. –  Ray Gulick Feb 7 '12 at 15:20
    
type should be date, not char. otherwise, post the SQL generated by this query, perhaps you can't sort on a key used in an OR relation? –  Milo Feb 7 '12 at 17:33
show 1 more comment

1 Answer

up vote 3 down vote accepted

I had this same problem until I realized you don't actually have to query by the event start date, only the end date.

As long as the event's end date has not past it doesn't matter whether the start date is in the future or not. All you have to do is grab all the dates that have not ended yet then you can order them by their start date.

This should work for you:

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $today = date('Y-m-d', strtotime('-6 hours'));
    $upcomingevents = new WP_Query(array(
        'post_type' => 'events', 
        'posts_per_page' => 6,  
        'paged' => $paged,
        'meta_query'=>array(
            array(
                'key' => 'evend_date',
                'value' => $today,
                'compare' => '>=',
                'type' => 'CHAR'
                )
             ),
        'meta_key' => 'evstart_date',
        'orderby' => 'meta_value',
        'order' => 'ASC'
    )); 
?>
share|improve this answer
    
I finally came to the same conclusion, Christian; hadn't gotten round to posting it as an answer. Thanks for your answer. –  Ray Gulick Feb 14 '12 at 7:58
    
Great answer Christian. Welcome to the site, keep providing good answers like this; the site needs them. –  DigitalSea Feb 15 '12 at 2:17
add comment

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.