wp query - WP_Query meta_query >= date

admin2025-06-04  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?

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?

Share Improve this question edited Jan 6, 2019 at 14:26 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jan 6, 2019 at 14:20 LudoLudo 612 silver badges11 bronze badges 1
  • Try adding 'type' => 'DATE', to the most inner array – kero Commented Jan 6, 2019 at 14:27
Add a comment  | 

1 Answer 1

Reset to default 3

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 );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749039307a315842.html

最新回复(0)