Search code examples
phpadvanced-custom-fieldsacfpro

How to Compare a Today Date with Null or Empty Date Field in ACF?


I am using ACF plugin to get the date and also it's not a mandatory field (Because there has another "To Be Announced field"). So if people didn't select that date, It will be showing a null value or empty field.

I want to compare today date with selected date from the ACF picker, Also want to check whether it has a Null value.

I tried following code. Please help me

$today = date ('Ymd');

$posts = get_posts(array(
    'post_type'         => 'events',
    'meta_query'             => array(
        array(
            'key'       => 'date_of_the_event',
            'value'     => $today,
            'compare'   => '<= || '' ',
        ),
    ),  
    'posts_per_page'    => -1,
    'meta_key'          => 'date_of_the_event',
    'orderby'           => 'meta_value',
    'order'             => 'DESC'
));

Solution

  • Remove || from compare and put it in relation like that

    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'date_of_the_event',
            'value' => false,
            'compare' => '<=' 
        ),
        array(
            'key' => 'date_of_the_event',
            'compare' => 'NOT EXISTS' 
        ),
    )