I have a query_posts call in a WP template. Through the use of the More Fields Plugin I can give the site admin the ability to create an event (custom post type) and then enter a date which is formatted: YYYY/mm/dd.
The main question is; what value should I pass to the value option in the meta_query array? I am currently trying to pass “date(“Y/m/d h:i A”)” (minus the quotes), because, as I understand it, that will print the current date today. I don’t care about the time of date so that may be irrelevant. Ulitimatly I am trying to use the compare option to nail down showing upcoming events, past events in different places on this site. In one other spot I actually need to pass the value option an array that prints that first and last day of the current month, limiting the output to events happening this month.
<?php
query_posts( array(
'post_type' => 'event', // only query events
'meta_key' => 'event_date', // load up the event_date meta
'orderby' => 'meta_value', // sort by the event_date
'order' => 'asc', // ascending, so earlier events first
'posts_per_page' => '2',
'meta_query' => array( // restrict posts based on meta values
'key' => 'event_date', // which meta to query
'value' => date("Y/m/d h:i A"), // value for comparison
'compare' => '>=', // method of comparison
'type' => 'DATE' // datatype, we don't want to compare the string values
) // end meta_query array
) // end array
); // close query_posts call
?>
I wound up working on the exact same thing and this post was very helpful. I used Custom Fields and here is the code that I used to create a list of all events greater than the current date. Note the extra taxonomy based filters.
It largely depends on how your date is stored in the meta value in the first place. In general, it is a good idea to store dates in MySQL as MySQL dates/timestamps.
MySQL timestamps have the format
Y-m-d h:i:s
.However, it is always a good idea to use WP’s own date mangling functions. As such, to get the current date in MySQL format, use
current_time('mysql')
.To format a MySQL date for display, use
mysql2date($format, $mysql_date)
.In this case it is best to display the date as configured in the settings, so use
$format = get_option('date_format');
.To store a user-selected date, you’ll have to transcode it into a MySQL date. To do so, the easiest – but not safest – way is
date('Y-m-d h:i:s', $unix_timestamp);
.$unix_timestamp
can often be derived viastrtotime($user_input)
.However,
strtotime()
doesn’t do sanity checks on it’s own, so it’s best to write your own converstion function.As for getting the month range, here’s a function i’m using to get the month boundaries for any MySQL timestamp:
If you want to get the week boundaries, WP already comes with a function for that:
get_weekstartend($time);
, which also delivers the boundaries as an array.You can then use these in your
meta_query
argument by doing two separate comparisons.I wound up going with the following. I setup a event-momth field and comparing from there. thanks for the help
Hi below I am posting my solution. Where I have stored date in
Y-m-d H:i
format (like 2013-07-31 16:45).Event which ending after Today will be queried only by
meta_query
.date_default_timezone_set('Asia/Calcutta');
I set default time zone for
date()
function.