1

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

3
  • I'm not sure about the answer, but i propose you use the orderby "meta_value_num" instead of "meta_value", so it doesn't use alphanumeric order. Jul 16, 2014 at 8:10
  • Hmm.. not quite. Just tried it. With meta_value_num date everything is orderd by mb_home ASC and then date DESC. O_o Jul 16, 2014 at 8:27
  • Actually, I'll change it to meta_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. Jul 16, 2014 at 10:33

1 Answer 1

1

While WordPress does currently support multiple orderby it doesn't actually support multiple order respectively.

This was fixed very recently, see ticket #17065 (Independent ASC/DESC in multiple ORDER BY statement.) and will be available in 4.0 release with new array syntax: 'orderby' => array( 'title' => 'DESC', 'menu_order' => 'ASC' ).

3
  • I'm fine with the actual functionalities of WP_Query, as I want both mb_home and date to be DESC. My problem is that I'm probably doing something wrong, as it does not work. Jul 16, 2014 at 10:21
  • @Alevalentini I am not sure how well that plays with meta query part :\ In your place I would dump SQL generated and see what is happening.
    – Rarst
    Jul 16, 2014 at 10:30
  • I added the query dump in first post edit. Jul 16, 2014 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.