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!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.