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
This happens because of the
OR
relation onmeta_query
and the way WordPress generates the actual query string. You need to leave out themeta_key
andorderby
from the query args and hook into theposts_clauses
filter to modify thewhere
andorderby
pieces of the query:Add the filter before setting up your WP_Query object and then make sure to remove it after running your query to not affect other queries:
UPDATE: Use
yy-mm-dd
format for your date fields on ACF settings. (yy-mm-dd
in JS/ACF ==Y-m-d
in PHP/MySQL datetime format) I have updated the code above for this format. (updated$current_date
format and added'type' => 'DATE'
inmeta_query
)