What is the correct way to compare dates in a WP query_posts meta_query.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?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', ) ) ) ); ?> |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.