In my args I would like to exclude posts where the datum_event is not in the future. Currently it only displays posts with datum_event values that are in the past, but because older posts don't have any value for datum_event, the old posts don't get shown.
I know how to compare against a single value, but how do I compare against all future dates?
Basically: EXCLUDE posts with datum_event
compare
future.
$args = array(
'post_type' => 'agenda',
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'datum_event',
'value' => date('Ymd'),
'compare' => '<='
)
),
);
In my args I would like to exclude posts where the datum_event is not in the future. Currently it only displays posts with datum_event values that are in the past, but because older posts don't have any value for datum_event, the old posts don't get shown.
I know how to compare against a single value, but how do I compare against all future dates?
Basically: EXCLUDE posts with datum_event
compare
future.
$args = array(
'post_type' => 'agenda',
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'datum_event',
'value' => date('Ymd'),
'compare' => '<='
)
),
);
You might try adding a second meta_query that would look for anything without the meta key:
$args = array(
'post_type' => 'agenda',
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'datum_event',
'value' => date('Ymd'),
'compare' => '<='
),
array(
'key' => 'datum_event',
'meta_compare' => 'NOT_EXISTS',
'value' => 'placeholder'
),
),
);
Note that the value of placeholder is required to the make the not exists work - see https://codex.wordpress/Class_Reference/WP_Query#Custom_Field_Parameters
This is untested code but hopefully it helps!
dateum_event
value in the past AND also, at the same time, you want to retrieve posts where thedatum_event
value is blank (does not exist)? Is that it? – filipecsweb Commented Jan 30, 2019 at 21:41