php - WP_Query to select custom post type with Advanced Custom Fields (ACF) date

admin2025-01-08  6

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);

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);
Share Improve this question asked Apr 21, 2021 at 15:29 ConnavearConnavear 112 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

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 code looks mostly correctly but you are using a meta_query to filter posts based on the "End Date" field, and you are combining conditions with an OR relation, which is appropriate for your case.

add adjustments code for work properly in your condition

Date Formatting: Ensure that the date format in the ACF field matches the format you're using in the query. It seems like you're using the format 'Ymd', which is for dates like 20240214 (year-month-day). Make sure this matches the format ACF stores the date in.

Comparison Operator: Adjust the comparison operator for posts without an "End Date". Instead of using '=', use 'NOT EXISTS'. This will include posts where the "End Date" field doesn't exist

$meta_query = array(
    'relation' => 'OR',
    array(
        'key' => 'vacancy_end_date',
        'value' => date('Ymd'), // Adjust format if needed
        'type' => 'DATE',
        'compare' => '>=',
    ),
    array(
        'key' => 'vacancy_end_date',
        'compare' => 'NOT EXISTS', // Include posts without an End Date
    ),
);

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

$posts_query = new WP_Query($args);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270626a1443.html

最新回复(0)