I'm struggling with a wp_query and i need your help.
I have 3 custom fields called "Agenda_day", "Agenda_month", "Agenda_year", representing the day, month and year of an event.
I want to order the results of my query by day, then month, and finally year, ascendingly.
Here is my query :
$query_agenda = new WP_Query(
array(
'posts_per_page' => 8,
'cat' => 4,
'meta_query' =>
array(
'relation' => 'AND',
'day' => array('key' => 'Agenda_day', 'compare' => 'EXISTS'),
'month' => array('key' => 'Agenda_month', 'compare' => 'EXISTS'),
'year' => array('key' => 'Agenda_year', 'compare' => 'EXISTS')
),
'orderby' => array('day' => 'ASC', 'month' => 'ASC', 'year' => 'ASC')
)
);
And this does not work ... can you explain me why and show me how to fix it ?
Thank you !
EDIT
Here is the executed query (results are returned but not well ordered)
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (4) ) AND ( wp_postmeta.meta_key = 'Agenda_jour' AND mt1.meta_key = 'Agenda_mois' AND mt2.meta_key = 'Agenda_annee' ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY CAST(wp_postmeta.meta_value AS CHAR) ASC, CAST(mt1.meta_value AS CHAR) ASC, CAST(mt2.meta_value AS CHAR) ASC LIMIT 0, 8
$query_agenda->request
after above code and see if query is being properly created or not? Then run that in your database and see the result. Or if possible please add that query to your question so i can get some idea. – Bhoomi