1

1st post on this forum so sorry if not enough detail passed.

I am trying to use WP_Query to gather CPT that have an Advanced Custom Field (WP Plugin) of "End Date", the field is a date picker. I want the Query to return all posts that are before their individual "End Date" and all posts that don't have an "End Date" incase the user doesn't know/want to add one. Basically, I don't want posts to make it through the query if the "End Date" has passed.

Below is what I have tried so far with little success

       $meta_query = array(
            'relation' => 'OR',
            array(
                'key' => 'vacancy_end_date',
                'value' => date('Ymd'),
                'type' => 'DATE',
                'compare' => '>='
            ),
            array(
                'key' => 'vacancy_end_date',
                'value' => '',
                'type' => 'DATE',
                'compare' => '='
            )
        );

        $args = [
            'post_type' => 'vacancy',
            // origionally 9
            'posts_per_page' => -1,
            'meta_key' => 'vacancy_end_date',
            'meta_query' => $meta_query,
        ];

        $posts = new \WP_Query($args);
0

In what form is vacancy_end_date stored in your database by ACF? Make sure you're asking for the same form. I.e. date('Ymd') produces 20200421 but your vacancy_end_date might be stored as unix stamp stamp, which would be a string like 1619028671 so you would never match it.

I do not think you need the 'meta_key' => 'vacancy_end_date', line in $args in addition to the meta_query.

https://generatewp.com/wp_meta_query/ suggests you do:

$meta_query = array(
    'relation' => 'OR',
    array(
        'key'     => 'vacancy_end_date',
        'value'   => $date,
        'compare' => '>=',
    ),
    array(
        'key'     => 'vacancy_end_date',
        'value'   => '',
        'compare' => '=',
    ),
);

$args = array(
    'post_type'              => 'vacancy',
    'meta_query'             => $meta_query,
);

$query = new WP_Query( $args );

You just need to define $date accurately, as mentioned about.

1
  • Thanks, I will have a look into this – Connavear Apr 22 at 8:07
0

This did the trick. Ymd was the correct date format. To get posts to show where no value had been added I just needed to refer to the CPT with no value and set it to NOT EXISTS.

        $meta_query = [
            'relation' => 'OR',
            [
                'key'     => 'vacancy_end_date',
                'value'   => date('Ymd'),
                'compare' => '>=',
            ],
            [
                'key'     => 'vacancy_end_date',
                'compare' => 'NOT EXISTS'
            ],
        ];

        $args = [
            'post_type'              => 'vacancy',
            'meta_query'             => $meta_query,
            'posts_per_page' => -1,
        ];

        $posts = new \WP_Query($args);

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.