I have a frustrating issue. I am using the date_query
as referenced in the Codex:
<?php
$today = getdate();
$args = array(
'post_type' => 'Lighting',
'post_status' => 'publish',
'posts_per_page' => 1,
'date_query' => array(
array(
'year' => $today['year'],
'month' => $today['mon'],
'day' => $today['mday'],
),
),
);
$the_query = new WP_Query( $args );
While I can display custom posts as expected if I exclude 'day'
, I get no posts when it is included in the query. I am using Advanced Custom Fields Pro with the date-picker
. I have no idea why this is happening, and I've searched tirelessly to determine why my date_query
is not working. I am able to echo the date, so I don't understand where the disconnect is.
/****** RESPONSE TO ANSWERS ******/
Thanks for the responses. I have done a meta_query with what I think is the proper date format, but I am still unable to query only today's post. Here is the new query:
<?php // Let's get the data we need to loop through below
$args = array(
'post_type' => 'carillon', // Tell WordPress which post type we want
'orderby' => 'meta_value', // We want to organize the events by date
'meta_key' => 'date_of_lighting', // Grab the "date_of_event" field created via "date-picker" plugin (stored in ‘YYYYMMDD’)
'posts_per_page' => '1', // Let's show just one post.
'meta_query' => array( // WordPress has all the results, now, return only the event on today's date
array(
'key' => 'date_of_lighting', // Check the s"date_of_lighting field
'value' => date("Y-M-D"), // Set today's date (note the similar format)
'compare' => '=', // Return only today's post
'type' => 'NUMERIC' // Let WordPress know we're working with numbers
)
)
);
Any suggestions? Thanks, again.
/******** SOLUTION **********/
Hi Everyone,
So, I found a solution that works. I changed the type to "DATE". It's interesting that in other examples where folks want to show todays date and beyond, they use a "Numeric" type. I guess it makes some sense, but I'm going to jump into the Codex to understand this more. I appreciate all of your help. Here is the solution that works:
<?php // Let's get the data we need to loop through below
$args = array(
'post_type' => 'carillon', // Tell WordPress which post type we want
'orderby' => 'meta_value', // We want to organize the events by date
'meta_key' => 'date_of_lighting', // Grab the "date_of_event" field created via "date-picker" plugin (stored in ‘YYYYMMDD’)
'posts_per_page' => '1', // Let's show just one post.
'meta_query' => array( // WordPress has all the results, now, return only the event for today's date
array(
'key' => 'date_of_lighting', // Check the s"date_of_lighting field
'value' => date("Y-m-d"), // Set today's date (note the similar format)
'compare' => '=', // Return only today's post
'type' => 'DATE' // Let WordPress know we're working with a date
)
)
);
getdate()
with notimestamp
parameter given returns the array for the current local time. It looks like you don't have any posts for the day you specify in the'date_query'
.meta_query
. Look at the codex at the custom field parameters inWP_Query
. Just a note, your date MUST BE saved asY-m-d
format or unix timestamp, any other format will NOT work, and then the date you pass as meta value MUST MATCH the fomat in which the field is saved otherwise this will also not work. There is really tons of info on-site as well, so be sure to use the site search as well