Search code examples
phpwordpressdateadvanced-custom-fieldsacfpro

WordPress get_posts() by ACF date field query returns nothing


I am trying to build a calendar with various "Time Blocks" as a custom post type. Each block has an ACF date picker field formatted as "m/d/Y". I'm trying to use get_posts to query all time blocks for a given day, for example today May 18, 2021.

My Query

$args = array( 
    'post_type'  => 'time_blocks',
    'meta_query' => array(
        
    array(
        'key'   => 'cb_date',
        'value' => '05/18/2021'
    )
) ); $blocks = get_posts($args);

All guides I've found online have been for date ranges, but in this case I'm trying to get posts that match a single date.

When using get_posts, this query returns nothing despite having a post saved with that exact value.


Solution

  • Check that the date data format within the database is in this "dd / mm / yyyy" (d / m / Y) format or that you have not saved it as "yyyy-mm-dd" (Y-m-d). I would also check using wp_query instead of get_posts