-1

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'
));
amarrajk
  • 46
  • 4
  • `'compare' => '<= || '' '` - you can not just invent your own abstruse compare "operator", and expect that to work. You will need to add two meta queries here, one that checks your date condition, and another one that allows for the date field value to be just null / empty, and combine them both with `'relation' => 'OR'` – CBroe Apr 05 '22 at 09:17

1 Answers1

1

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' 
    ),
)
Fabrice Fabiyi
  • 351
  • 6
  • 17