Display posts with a start OR end date later than current date

I’m working with an upcoming events list. I create a custom post type, with a custom field: [event_start_date]. I also have some multiday extension events, defined by an additional custom field: [event_end_date]. Then, I have this two kinds of events (examples):

  • Birthday | start date: 13/08/2012, end_date: ’empty’
  • Holidays | start date: 5/08/2012, end_date: 30/08/2012

I would like to display events with a ‘start’ or ‘end date’ later than current time, but I failed.

Read More

I think my date format is correct, because I had started using other ‘query’ that works perfect only using the [start date]. My problem is incorporating the ‘OR’ end_date condition. I tried with a meta_query with an ‘OR relation’, but doesn’t work:

$args = array(
             'post_type' => 'eventos2',
             'posts_per_page' => 4,
             'meta_key' => 'event_start_date',
             'orderby' => 'meta_value',
             'order' => 'ASC',
             'meta_query' => array(
             'relation' => 'OR',
             array('key'  => 'event_start_date',
                   'value' => date('Y/m/d', strtotime('-1 day')),
                   'compare' => '>=')

             array('key'  => 'event_end_date',
                   'value' => date('Y/m/d', strtotime('-1 day')),
                   'compare' => '>=')
             )
   );
$loop = new WP_Query( $args ); 
while ( $loop->have_posts() ) : $loop->the_post();
$end = DateTime::createFromFormat('Y/m/d', get_field('event_end_date')); 
$start = DateTime::createFromFormat('Y/m/d', get_field('event_start_date'));

if ($end !='') {  /*print events with start and end_date*/ 
    echo "<li>";
    echo the_title(), "</br>";
    echo $start-> format('Y/m/d'),"&nbsp". to . "&nbsp". $end-> format('Y/m/d'); 
    echo "</li>";
    }
    else {             /*print events with only start_date*/
    echo "<li>";
    echo the_title(), "</br>";
    echo $start-> format('Y/m/d');
    echo "</li>";   
    }
    endwhile;
   ?>

It show this result:

  • EVENT 2 |
    2012/08/08 to 2012/08/15
  • EVENT 4 |
    2012/08/31 to 2012/09/06
  • EVENT 3 |
    2012/08/17
  • EVENT 1 |
    2012/08/01

How you can see, EVENT 1 is listed, despite of is a past event. Moreover events are in disorder.

I will appreciate any orientation with the problem.

Related posts

Leave a Reply

1 comment

  1. Edit:

    I believe my comments below are still valid, but I suspect part of the problem is the

    'relation' => 'OR',
    

    This should be ‘AND’ (it’s default value,so it can be removed) to ensure that it only returns posts which satisfy both conditions. Currently it will return events that start, or end, after yesterday. In effect, this returns all events that end yesterday, or after.

    Of course this may still only be part of the problem…


    I can’t see anything wrong with the query itself, but the string representation of your events’ dates will cause query problems (without examples it’s hard to say if this is the problem). If you are going to sort by a string representation of dates rather than a timestamp, the format m/d/Y won’t sort properly:

    01/01/2012 occurs after, but appears before

    11/01/1012

    for instance! The format Y/m/d will work.