This is similar to, but more specific than, the question found here. That question suggests the WordPress WP_Query example, which are helpful, but they’re not working for me.
My code is as follows:
$posts = get_posts(array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'from_datetime',
'meta_value' => date( "F d, Y g:i a" ),
'meta_compare' => '>',
'orderby' => 'meta_value',
'order' => 'ASC'
));
The custom fields are made using ACF in our blog. The from_datetime
field shows like this:
April 18, 2017 2:30 pm
So I’ve translated this to the date nomenclature:
date( "F d, Y g:i a" )
Events are a custom post type. Basically I want to show the upcoming 2 events that are NOT passed. So this is quite close to the example on the WP_Query page, but still the above doesn’t work. The query returns nothing. I do know that if I remove the meta_value
stuff above, there are four events to show that are beyond “now”.
Any thoughts on what I may be doing wrongly? The query seems correct according to the documentation at WP. Thank you!
2 Answers
I think you have error in your query. Please try this:
$posts = get_posts(array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_query' => array(
'meta_key' => 'from_datetime',
'type' => 'DATETIME', // You can also try changing it to TIME or DATE if it doesn't work
'meta_value' => date( "F d, Y g:i a" ),
'meta_compare' => '>',
),
'orderby' => 'meta_value',
'order' => 'ASC'
));