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 .
I have a code that work fine for events with a start_date later than today. But I fail incorporating the end_date condition. I try with a meta_query, with OR relation, that currently don’t work:
$args = array(
'post_type' => 'events',
'posts_per_page' => 4,
'orderby' => 'event_start_date',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array('key' => 'event_start_date',
'value' => date('m/d/Y', strtotime('-1 day')),
'compare' => '>='),
array('key' => 'event_end_date',
'value' => date('m/d/Y', strtotime('-1 day')),
'compare' => '>=')
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
// Rest of the loop
Thanks in advance.
Note: I have this other query, that works perfect selecting events with only a start date > than current date.
$args = array(
'post_type' => 'events',
'posts_per_page' => 4,
'meta_key' => 'event_start_date',
'meta_value' => date('m/d/Y', strtotime('-1 day')),
'meta_compare' => '>=',
'orderby' => 'event_start_date',
'order' => 'ASC',
); // The same loop and query
Actually to order by a meta key the
meta-key
must be present in the query and use themeta_value
asorderby
value.Also you can use a type in your each array of meta_query as follows
Update: You should use
date('m/d/Y')
and'compare' => '>'
and make sure your date format is correct because right now it’s in (month/day/year).date('m/d/Y', strtotime('-1 day'))
will show past dates too.This should work if everything else is correct.
Read Here.
Solution not worked for me (mybe topic is too old for older versions).
For next searcher – solution to show all past events (with pagination)