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.
<?php // Let's get the data we need to loop through below
$events = new WP_Query(
array(
'post_type' => 'event', // Tell WordPress which post type we want
'orderby' => 'meta_value', // We want to organize the events by date
'meta_key' => 'event-start-date', // Grab the "start date" field created via "More Fields" plugin (stored in YYYY-MM-DD format)
'order' => 'ASC', // ASC is the other option
'posts_per_page' => '-1', // Let's show them all.
'meta_query' => array( // WordPress has all the results, now, return only the events after today's date
array(
'key' => 'event-start-date', // Check the start date field
'value' => date("Y-m-d"), // Set today's date (note the similar format)
'compare' => '>=', // Return the ones greater than today's date
'type' => 'DATE' // Let WordPress know we're working with date
)
),
'tax_query' => array( // Return only concerts (event-types) and events where "songs-of-ascent" is performing
array(
'taxonomy' => 'event-types',
'field' => 'slug',
'terms' => 'concert',
),
array(
'taxonomy' => 'speakers',
'field' => 'slug',
'terms' => 'songs-of-ascent',
)
)
)
);
?>