0

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' => '<='
        )
    ),
);
  • I'm not sure I understood you. You are saying you want to retrieve all posts which have the dateum_event value in the past AND also, at the same time, you want to retrieve posts where the datum_event value is blank (does not exist)? Is that it? – filipecsweb Jan 30 at 21:41
  • Sorry I forgot to type 'not' :-) I ment I want retrieve all posts which have the datum_event NOT in the future. @filipecsweb – Ludo Jan 30 at 21:44
  • And the answer below does not fit you? – filipecsweb Jan 30 at 21:56
  • Let me know if it does not work. – filipecsweb Jan 31 at 1:17
2

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.org/Class_Reference/WP_Query#Custom_Field_Parameters

This is untested code but hopefully it helps!

  • How would you expand this functionality to order by normal post date if the post has no value for 'datum_event'? - I'd like to know this for a page with the same query but the sql on this page also includes two other posttypes that do not have the 'datum_event' field at all. – Ludo Jan 31 at 16:27
  • I believe you can just use the orderby param - codex.wordpress.org/Class_Reference/… – Welcher Jan 31 at 18:26
  • Would you be so kind te explain how @welcher? See my separate question here: wordpress.stackexchange.com/questions/327318/… – Ludo Jan 31 at 23:56
  • The code above doesn't work. I have to remove the upper meta_query array so that the posts without data in datum_event are displayed.. – Ludo Feb 1 at 0:46
  • @Ludo looks like your other question has been deleted. As to the code above not working, I did mention it was untested. I do believe the theory behind it is correct ( using the relation param with 2 query types ) but you may need to adjust some of the meta_query items. – Welcher Feb 1 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.