Last updated on September 16, 2023

ACF date picker not working in query

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Making ACF Date Picker Work in Queries.

The code snippet provided below demonstrates how to use the Advanced Custom Fields (ACF) date picker field in a WordPress query. This is useful when you want to retrieve posts based on a specific date range using the ACF date picker field.

$selected_date = get_field('your_date_picker_field');
$start_date = date('Y-m-d', strtotime($selected_date));
$end_date = date('Y-m-d', strtotime($selected_date . ' +1 day'));

$args = array(
    'post_type' => 'your_post_type',
    'meta_query' => array(
        array(
            'key' => 'your_date_picker_field',
            'value' => array($start_date, $end_date),
            'compare' => 'BETWEEN',
            'type' => 'DATE',
        ),
    ),
);

$query = new WP_Query($args);

In this code snippet, we first retrieve the selected date from the ACF date picker field using the get_field() function. We then convert the selected date into the desired format for comparison in the query. The $start_date represents the selected date, and the $end_date is calculated by adding one day to the selected date.

Next, we define the query arguments using the $args array. We set the post_type parameter to specify the post type you want to query. In the meta_query parameter, we define a meta query to filter the posts based on the date range. The key parameter should be set to the ACF date picker field name, the value parameter is an array containing the start and end dates, the compare parameter is set to ‘BETWEEN’ to include posts within the specified date range, and the type parameter is set to ‘DATE’ to ensure proper date comparison.

Finally, we create a new instance of WP_Query with the defined arguments to execute the query and retrieve the desired posts.

Note: Make sure to replace 'your_date_picker_field' with the actual ACF date picker field name and 'your_post_type' with the desired post type you want to query.

Examples

Example 1: Querying posts based on ACF date picker value

This example demonstrates how to query posts based on the value selected in an ACF date picker field. The code snippet below shows how to use the meta_query parameter in a WP_Query to filter posts based on the ACF date picker value.

$args = array(
    'post_type' => 'post',
    'meta_query' => array(
        array(
            'key' => 'event_date', // ACF date picker field key
            'value' => date('Ymd'), // Current date in Ymd format
            'compare' => '>=', // Query posts with date greater than or equal to current date
            'type' => 'DATE'
        )
    )
);

$query = new WP_Query( $args );

In this example, we are querying posts of the post post type and using the meta_query parameter to filter based on the ACF date picker field with the key event_date. We compare the value of the field with the current date (date('Ymd')) using the >= operator to retrieve posts with a date greater than or equal to the current date.

Example 2: Querying posts within a date range

This example demonstrates how to query posts within a specific date range using the ACF date picker field. The code snippet below shows how to use the meta_query parameter with multiple conditions to filter posts based on a date range.

$start_date = '2022-01-01';
$end_date = '2022-12-31';

$args = array(
    'post_type' => 'post',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'event_date', // ACF date picker field key
            'value' => $start_date,
            'compare' => '>=',
            'type' => 'DATE'
        ),
        array(
            'key' => 'event_date',
            'value' => $end_date,
            'compare' => '<=',
            'type' => 'DATE'
        )
    )
);

$query = new WP_Query( $args );

In this example, we are querying posts of the post post type and using the meta_query parameter with two conditions. The first condition checks if the ACF date picker field value (event_date) is greater than or equal to the $start_date, and the second condition checks if it is less than or equal to the $end_date. By specifying the 'relation' => 'AND', both conditions must be met for a post to be included in the query results.

Example 3: Sorting posts by ACF date picker value

This example demonstrates how to sort posts based on the value selected in an ACF date picker field. The code snippet below shows how to use the meta_key and orderby parameters in a WP_Query to sort posts based on the ACF date picker value.

$args = array(
    'post_type' => 'post',
    'meta_key' => 'event_date', // ACF date picker field key
    'orderby' => 'meta_value',
    'order' => 'ASC'
);

$query = new WP_Query( $args );

In this example, we are querying posts of the post post type and using the meta_key parameter to specify the ACF date picker field key (event_date). We set the orderby parameter to 'meta_value' to indicate that we want to sort the posts based on the value of the ACF date picker field. The order parameter is set to 'ASC' to sort the posts in ascending order based on the date values.

Last updated on September 16, 2023. Originally posted on September 16, 2023.

Leave a Reply