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

Christian Gundersson christian at buro2.se
Tue Apr 5 16:23:29 UTC 2011


Hi,

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.

Still open to other suggestions though if I don't manage to get this working
:)

Regards,
//Christian, Büro 2




2011/4/5 Philip Walton <philip at philipwalton.com>

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



-- 
Mvh
//Christian, Büro 2

Büro 2
Skolgatan 5
262 31 Ängelholm
0763-91 55 85
christian at buro2.se


More information about the wp-hackers mailing list