Support

Account

Home Forums Front-end Issues meta_query conditions not working on simple ACF fields

Helping

meta_query conditions not working on simple ACF fields

  • We have a custom post type, events. This has a custom field called event_start.

    In trying to query only past events, I’d like to limit the query with this basic logic:

    – post type is events
    – event_start is not empty
    – event_start is a date before today

    For this I’ve composed what seems to be a straightforward query:

    
        $args = array(
          'post_type'        => 'events',
          'posts_per_page'   => -1,
          'meta_query' => array(
                   'relation' => 'AND',
                    array(
                        'key'     => 'event_start',
                        'value'   => $today,
                        'compare' => '<'
                    ),
                    array(
                        'key'     => 'event_start',
                        'compare' => 'EXISTS' 
                    ),
                    array(
                        'key'     => 'event_start',
                        'compare' => '!=',
                        'value'   => ''
                    ),
                ),
        );
        
        $query = new WP_Query($args);
    

    But this is returning all the events. What am I missing? Appreciate any pointers, thanks!

  • how are you creating $today, the format must be “Ymd” for a date field

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.