Support

Account

Home Forums General Issues Query posts after "today" using ACF Date

Solved

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);
    ?>
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Query posts after "today" using ACF Date’ is closed to new replies.