Home › Forums › General Issues › Filtering by date › Reply To: Filtering by date
I’m late to the party, but I arrived here after looking for an answer I knew existed.
The answer can be found here.
<?php
// find date time now
$date_now = date('Y-m-d H:i:s');
// query events
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'event',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '<=',
'value' => $date_now,
'type' => 'DATETIME'
),
array(
'key' => 'end_date',
'compare' => '>=',
'value' => $date_now,
'type' => 'DATETIME'
)
),
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'start_date',
'meta_type' => 'DATE'
));
if( $posts ): ?>
<h2>Events on right now</h2>
<ul id="events">
<?php foreach( $posts as $p ): ?>
<li>
<strong><?php echo $p->post_title; ?></strong>: <?php the_field('start_date', $p->ID); ?> - <?php the_field('end_date', $p->ID); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
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.