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);
?>
You must be logged in to reply to this topic.
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!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.