Home › Forums › General Issues › Query posts after "today" using ACF Date
Hi guys!,
I have a problem. I have this code:
<?php
$today = date('dmY');
// WP_Query arguments
$args = array (
'posts_per_page' => -1,
'post_type' => 'dates',
'meta_query' => array(
array(
'key' => 'my_custom_field_with_date',
'value' => $today,
'compare' => '>'
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php echo the_title(); ?></p>
<?
}
} else {
?>
<p>No dates found.</p>
<?
}
// Restore original Post Data
wp_reset_postdata();
?>
So, I have three dates, with ACF dates after “today”, however don’t show it.
The output of ACF Date and date() have the same date format.
I would appreciate any help.
Thanks.
Hi @rodsaurio
Could you please tell me which version of ACF did you use? If you used the PRO version, you need to compare it with this format:
$today = date('Ymd');
That’s because the ACF PRO saves the date in that format even if you set the Display/Return Format to anything else.
I hope this helps π
Hi James,
Thank you for your comment. I’m using the ACF 4.4.7, but anyway, I tried your solution and solved my problem.
Finally I used $today = date(Ymd);
and the save format of the field yymmdd
. This worked for me.
Thank you for help me π
Hello, I found this post but it doesn’t work for me. I have custom posts with an ACF date field “event_date”. I post a whole month’s worth of these, but I only want to show the ones with event_date after today (to not show ones that happened already).
I am using ACF 5.8.7 (not pro).
My query follows, it shows all events even ones in the past:
<?php
// $today = new DateTime(date('Y-m-d'));
$today = date('Ymd');
$start_date = date('Ym01');
$end_date = date('Ymt');
$meta_query = array(
'key' => 'event_date',
'value' => $today,
'type' => 'numeric',
'compare' => '>',
);?>
<?php
$args = array(
'post_type' => 'hh_event',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'event_date',
'order' => 'ASC',
);
$the_query = new WP_Query($args);
?>
Figured it out:
<?php
$today = date('Ymd');
$args = array(
'post_type' => 'hh_event',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => $today,
'compare' => '>',
),
),
'order' => '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!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PROβs Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 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.