Home › Forums › General Issues › Query posts where end and/or start date are in the past › Reply To: Query posts where end and/or start date are in the past
Wow thanks, that works perfectly!
I ended up doing this:
/* Ongoing Past Events */
$ongoing = new WP_Query( array(
'post_type' => 'event',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'acf_event_date_start',
'value' => date('Ymd'),
'compare' => '<',
'type' => 'date'
),
array(
'key' => 'acf_event_date_end',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'date'
)
),
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
if ($ongoing->have_posts()) :
$exclude = array();
while($ongoing->have_posts()) : $ongoing->the_post();
$exclude[] = $post->ID;
endwhile;
endif;
/* Past Events */
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query( array(
'post_type' => 'event',
'post__not_in' => $exclude, // Exclude ongoing past events
'meta_query' => array(
array(
'key' => 'acf_event_date_start',
'value' => date('Ymd'),
'compare' => '<',
'type' => 'date'
)
),
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => $paged
));
Which also works, but you need 2 queries which is not ideal.
Thanks for your help!
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!
🚀 ACF & ACF PRO 6.0.7 are now available.
— Advanced Custom Fields (@wp_acf) January 18, 2023
✨This release contains bug fixes and improvements while we continue to work on the next major release of ACF.https://t.co/wQgAOpwmUI
© 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.