Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom category page with the following query:

$args = array(  
   'cat'  => $portfolioCategories,   
   'posts_per_page'     => $itemsPerPage,  
   'orderby'            => 'menu_order date',   
   'order'              => 'DESC',   
   'paged'              => $paged   
);   
query_posts( $args );

So 'orderby' => 'menu_order' works just fine. 'orderby' => 'date' works just fine.

But 'orderby' => 'menu_order date' does not work - I get some weird completely random order.

What have I done wrong?

PS: Yes, I have enabled page attributes (ie, menu order) for posts in my functions file
PPS: I get the same results whether I use query_posts($args) or new WP_Query($args)

EDIT: The SQL generated looks like this:

SELECT SQL_CALC_FOUND_ROWS  wp_rk2ukj_posts.ID 
  FROM wp_rk2ukj_posts 
 INNER JOIN wp_rk2ukj_term_relationships ON (wp_rk2ukj_posts.ID = wp_rk2ukj_term_relationships.object_id) 
 WHERE 1=1  
   AND ( wp_rk2ukj_term_relationships.term_taxonomy_id IN (89,93,94,95,96,106) ) 
   AND wp_rk2ukj_posts.post_type = 'post' 
   AND (wp_rk2ukj_posts.post_status = 'publish' OR wp_rk2ukj_posts.post_status = 'private') 
 GROUP BY wp_rk2ukj_posts.ID
 ORDER BY wp_rk2ukj_posts.menu_order,wp_rk2ukj_posts.post_date DESC
 LIMIT 0, 9
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

This should help:

You can use multiple fields in orderby argument. Codex shows you how to do it (and you do it correctly):

$query = new WP_Query( array( 'post_type' => 'page', 'orderby' => 'menu_order date', 'order' => 'ASC' ) );

And it should solve your problem. (It should, but if you want to sort DESC it probably won't, because of this WordPress bug: http://core.trac.wordpress.org/ticket/17065)

Of course you always can use posts_orderby filter to create your own ORDER BY part of query.

function my_posts_orderby($orderby_statement) {
    $orderby_statement = "YOUR ORDER BY STATEMENT";
    return $orderby_statement;
}
add_filter('posts_orderby', 'my_posts_orderby');

Just add this filter just before you call your query and remove it after.

share|improve this answer
    
Even if I use new WP_Query instead of query_posts, it still doesn't work. I do want both parameters ordered DESC (so independent sorting is not an issue). I'm wondering if it isn't the GROUPBY clause being returned in the SQL? –  Amanda Aug 7 '13 at 9:39
1  
Read carefully pls. It's a bug in WordPress code. Multiple orderby won't work, if you use DESC order. The only way to do it now is to use posts_orderby filter. –  Krzysiek Dróżdż Aug 7 '13 at 9:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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