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

Time:04-06

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'
));

CodePudding user response:

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' 
    ),
)
  • Related