Simpler CODE:
Logically, when you check >=
for both _event_start_date
and _event_end_date
, that automatically covers the BETWEEN
check.
So the following CODE should work:
$termName = get_queried_object()->name;
$termSlug = get_queried_object()->slug;
$event1 = current_time( 'Y-m-d' );
$args = array(
'post_type' => 'event',
'event-categories' => $termSlug,
'post_status' => 'publish',
'posts_per_page' => 10,
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_event_start_date',
'value' => $event1,
'compare' => '>=',
'type' => 'DATE',
),
array(
'key' => '_event_end_date',
'value' => $event1,
'compare' => '>=',
'type' => 'DATE',
)
),
'orderby' => '_event_start_date',
);
$events = new WP_Query( $args );
echo $events->post_count;
Date check:
Also, make sure the date value $event1
is properly set as the one stored in Database (in post meta table). Normally, WordPress internally considers all date
to be in the GMT
timezone. However, that may vary depending on how the _event_start_date
and _event_end_date
is saved in your case. It can be different depending on the stored value:
// MySQL DATETIME field format
$event1 = current_time( 'mysql' );
// MySQL DATETIME field format in GMT
$event1 = current_time( 'mysql', 1 );
// Unix timestamp in local time zone
$event1 = current_time( 'timestamp' );
// Unix timestamp in GMT
$event1 = current_time( 'timestamp', 1 );
So, better check how that data is saved, or test with these variances. If date
fields include time as well, then you may also work with DATETIME
type.
Also, a note from codex:
The ‘type’ DATE works with the ‘compare’ value BETWEEN only if the date is stored at the format
YYYY-MM-DD
and tested with this format.
tax
Query :
The old tax
query is deprecated. So instead of using:
'event-categories' => $termSlug,
use:
'tax_query' => array(
array(
'taxonomy' => 'event-categories',
'field' => 'slug',
'terms' => $termSlug,
),
),