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

Otto otto at ottodestruct.com
Tue Apr 5 16:28:47 UTC 2011


On Tue, Apr 5, 2011 at 11:23 AM, Christian Gundersson
<christian at buro2.se> wrote:
> Thanks for the replys guys, I was hoping that it could be done with WP_Query
> so I wouldn't have to resort to PHP but it looks like that'll have to do. I
> wanted to avoid the whole add_filter() way because right now this isn't a
> plugin, but I might have to resort to that.

Can't do it with a naive WP_Query. Use the posts_orderby filter to add
your own ordering string.

function my_order($orderby) {
global $wpdb;
return "{$wpdb->posts.post_author} ASC, {$wpdb->posts.post_date} DESC";
}

add_filter( 'posts_orderby', 'my_order' );
$blah = new WP_Query(...);
remove_filter( 'posts_orderby', 'my_order' );

-Otto


More information about the wp-hackers mailing list