Hi guys,
This might be an easy for you gurus but it might also be very useful to heaps of other people who probably will crack their heads trying to figure this out.
I’m trying to get my upcoming events list to also display events which have end dates that have not passed today, and sort eveything in the list by start date.
Using datepicker, my start date is event_start and end date is event_end
How do I modify what I’ve got to get this? At the moment I can display all upcoming events, but I can’t display an event which has already started, but hasn’t ended yet.
Here’s my code :
<?php
$now = date('Y-m-d H:i:s');
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'event_start',
'compare' => '>=',
'value' => $now,
'type' => 'DATETIME'
)
),
'orderby' => 'meta_value_num'
);
$the_query = new WP_Query($args);
?>
Hi @sanny,
here my suggestion:
<?php
$now = date('Y-m-d H:i:s');
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
'date_upcoming_clause' => array(
'key' => 'event_start',
'compare' => '>=',
'value' => $now,
'type' => 'DATETIME'
),
array(
'relation' => 'AND',
'date_started_clause' => array(
'key' => 'event_start',
'compare' => '<=',
'value' => $now,
'type' => 'DATETIME'
),
'date_end_clause' => array(
'key' => 'event_end',
'compare' => '>=',
'value' => $now,
'type' => 'DATETIME'
),
),
),
'orderby' => array(
'date_started_clause' => 'ASC',
'date_end_clause' => 'ASC',
'date_upcoming_clause' => 'ASC',
),
);
$the_query = new WP_Query($args);
?>
The topic ‘Upcoming events (and events that haven't passed it's end date)’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.