I have searched but not found an answer to my problem. I have posts with a meta key used for ordering. I want to order post by this value. If they have same value I want them to be ordered by date.
I guessed that this was the query to use:
new WP_Query( array(
'post_type' => array ('post', 'aggregato'),
'cat' => '-1',
'posts_per_page' => 12,
'orderby' => 'meta_value_num date',
'order' => DESC,
'meta_key' => 'mb_home',
'meta_query' => array(
array(
'key' => 'mb_home',
'value' => 0,
'compare' => '!=',
)
)
));
(the 'cat' => '-1'
is just another check to exclude a specific category)
Now, this query returns posts that has a 'mb_home' value different than 0. And it orders them by that value. BUT completely ignores 'date'. If I have two posts with same mb_home value and different dates, it just goes date ASC instead of date DESC as expected.
Any pointers? What am I doing wrong here?
EDIT: This is the dump.
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
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)
WHERE 1=1 AND ( wp_posts.ID NOT IN ( SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (1) ) ) AND wp_posts.post_type IN ('post', 'aggregato') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') AND (wp_postmeta.meta_key = 'mb_home' AND (mt1.meta_key = 'mb_home' AND CAST(mt1.meta_value AS CHAR) > '0') ) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0,wp_posts.post_date DESC LIMIT 0, 12
meta_value_num date
everything is orderd bymb_home ASC
and thendate DESC
. O_ometa_value_num
, because yes, it's more effective in my case. If I order only by meta value num it works. As I add date, it goes bonkers.