$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Query post with meta_query where date is not in future|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Query post with meta_query where date is not in future

matteradmin10PV0评论

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' => '<='
        )
    ),
);
Share Improve this question edited Jan 30, 2019 at 21:48 Ludo asked Jan 30, 2019 at 21:31 LudoLudo 612 silver badges11 bronze badges 4
  • 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 Commented Jan 30, 2019 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 Commented Jan 30, 2019 at 21:44
  • And the answer below does not fit you? – filipecsweb Commented Jan 30, 2019 at 21:56
  • Let me know if it does not work. – filipecsweb Commented Jan 31, 2019 at 1:17
Add a comment  | 

1 Answer 1

Reset to default 4

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!

Post a comment

comment list (0)

  1. No comments so far