Using the Custom Post Type UI extension, I've created a 'Event' custom post-type, which fields are populated using custom fields (created with the extension Advanced Custom Fields) such as 'date-debut' and 'date-fin'.
On an archive page dedicated to this post-type, I'm trying to display the 10 next (current or future) events, ordered chronologically by beginning date.
The sorting part seems to be OK by itself; but it doesn't work anymore when I add the tests on dates, and anyway those don't work either.
Here is my code:
$current_date = date('ymd');
$my_query = new WP_Query( array (
'post_type' => 'mna_event',
'posts_per_page' => 10,
'orderby' => 'meta_value_num',
'meta_key' => 'date_debut',
'order' => 'ASC',
'meta_query' => array (
'relation' => 'OR',
array (
'key' => 'date_debut',
'value' => $current_date,
'compare' => '>='
),
array (
'key' => 'date_fin',
'value' => $current_date,
'compare' => '>='
)
)
)
);
Advice on the matter would be very much appreciated. Thanks
>=
your finish date. That doesn't look right.type
key formeta_query
withDATE
orDATETIME
. Check the codex BTW what is the format of date in the meta value?'date_fin' >= $current_date
you'll get all current and future events, without querying for 'date_debut' your query became simpler and so better performant. (2) Second issue is the way you are saving date, what format are you using? If you are storing date in mysql datetime formatY-m-d H:i:s
set meta_query type toDATETIME
(how @Chittaranjan suggests) if you are storing date in other formats, you have to change meta_query type accordingly e.g. for timestamps you have to set type toNUMERIC