Home › Forums › General Issues › Get post with the next date (ACF field) from now › Reply To: Get post with the next date (ACF field) from now
You’ll want to query the database using the custom field’s name as a meta key and comparing the values against your current custom field’s value. Try this:
<?php
$current_event_date = get_field('date');
$next_event_query = new WP_Query(array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'date',
'compare' => '>',
'value' => $current_event_date,
'type' => 'DATE'
)
),
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
if ($next_event_query->have_posts()) while ($next_event_query->have_posts()): $next_event_query->the_post();
// Print information about next event.
endwhile;
?>
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!
Our guide looks into WordPress custom fields and how they stack up against the possibilities of ACF.
— Advanced Custom Fields (@wp_acf) July 18, 2022
https://t.co/hk3yibkHyk
© 2022 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.