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.
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')," ". to . " ". $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.
relation=>'OR'
problem is only part of the problem.