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

Christian Gundersson christian at buro2.se
Wed Apr 6 06:09:29 UTC 2011


Hi!

Would this work if I put the function in functions.php and then the rest in
a template file?

Thanks again for all the help guys!

Regards,
//Christian Büro 2



2011/4/5 Otto <otto at ottodestruct.com>

> 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
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list