1

I have been trying for hours, I don't get my query to work: display only posts (from custom post type) with a field (Advanced Custom Fields date picker) value that is more than today.

echo date('d-m-Y'); prints the same date format as the posts output the_field('datum_event'); : 06-01-2019

$args = array(
    'post_type'  => 'agenda',
    'meta_query' => array(
        array(
            'key'     => 'datum_event',
            'value'   => '06-01-2019',
            'compare' => '>='
        )
    ),
    );
$loop = new WP_Query( $args );

What is missing?

  • Try adding 'type' => 'DATE', to the most inner array – kero Jan 6 at 14:27
1

What is missing? One tiny detail... And it's easy to overlook...

the_field('datum_event');

Prints the field using the format you've defined in field settings. But... It has nothing to do with how the value of that field is stored in DB. ACF uses YYYYMMDD format when storing date values in DB. And WP_Query doesn't use field format, since it looks directly in DB.

So you have to use other/raw format in your WP_Query:

$args = array(
    'post_type'  => 'agenda',
    'meta_query' => array(
        array(
            'key'     => 'datum_event',
            'value'   => '20190106',
            'compare' => '>='
        )
    ),
);
$loop = new WP_Query( $args );
  • Ah that's grand! Now using 'value' => date('Ymd'), and it's working! – Ludo Jan 6 at 14:43
  • Exactly, this is proper date format :) – Krzysiek Dróżdż Jan 6 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.