[wp-hackers] Multiple orderby and order options in WP_Query

Philip Walton philip at philipwalton.com
Tue Apr 5 15:58:56 UTC 2011


> A raw SQL query would do this for you ... "ORDER BY author ASC, date DESC"
> but I'm not sure if WP_Query supports that.  That's why I suggested ordering
> in PHP over just passing a straight query.
>
> Can anyone confirm whether or not WP_Query can handle multiple column
> ordering like this?
As far as I know, WP_Query just concatenates the ORDER and ORDER BY 
variables together, so you couldn't naively do what you're asking.

If I were you, I'd write a filter that reorganized the ORDER and ORDER 
BY parts. Then, once the query is run, remove the filter. For example:

$order = column_a, column_b;
$orderby = ASC, DESC;
$my_query = new WP_Query;
$query = array(
     'post_type'=>'post',
     'order' => $order,
     'order_by' => $orderby,
);

add_filter( 'query', 'filter_query' );
$my_query->query( $query );
remove_filter( 'query', 'filter_query' );

function filter_query( $query ) {
     // do query logic
     return $query;
}


More information about the wp-hackers mailing list