|
Hi!
I have a situation where I want to order a list of post both by author and date using WP_Query which isn't a problem since 'orderby' lets me do that. But I want the author part sorted ascending and date part sorted descending. Any tips on how I can achieve this? The arguments I pass to WP_Query are: $author_args = array ( 'post_type' => 'post', 'posts_per_page' => '-1', 'orderby' => 'author date', 'order' => 'ASC', 'nopaging' => true, 'category_name' => $category_name ); -- Regards //Christian, Büro 2 Büro 2 Skolgatan 5 262 31 Ängelholm 0763-91 55 85 [hidden email] _______________________________________________ wp-hackers mailing list [hidden email] http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
Christian,
My recommendation would be to use one orderby statement in the query, then filter the array returned to order by your second parameter. MySQL does support ordering by multiple columns, but not in the sense of ordering one ASC and the other DESC. So separate your two paradigms, let MySQL take care of one for you, then manually do the other via PHP. On Tue, Apr 5, 2011 at 5:43 AM, Christian Gundersson <[hidden email]>wrote: > Hi! > > I have a situation where I want to order a list of post both by author and > date using WP_Query which isn't a problem since 'orderby' lets me do that. > But I want the author part sorted ascending and date part sorted > descending. > > > Any tips on how I can achieve this? > > The arguments I pass to WP_Query are: > > $author_args = array ( > 'post_type' => 'post', > 'posts_per_page' => '-1', > 'orderby' => 'author date', > 'order' => 'ASC', > 'nopaging' => true, > 'category_name' => $category_name > ); > > -- > Regards > //Christian, Büro 2 > > Büro 2 > Skolgatan 5 > 262 31 Ängelholm > 0763-91 55 85 > [hidden email] > _______________________________________________ > wp-hackers mailing list > [hidden email] > http://lists.automattic.com/mailman/listinfo/wp-hackers > wp-hackers mailing list [hidden email] http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
MySql support this so there is no need to do it in PHP.
Patrik > MySQL does support ordering by multiple columns, but not in the sense of > ordering one ASC and the other DESC. So separate your two paradigms, let > MySQL take care of one for you, then manually do the other via PHP. > _______________________________________________ wp-hackers mailing list [hidden email] http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
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? On Tue, Apr 5, 2011 at 7:07 AM, <[hidden email]> wrote: > MySql support this so there is no need to do it in PHP. > > Patrik > > > MySQL does support ordering by multiple columns, but not in the sense of > > ordering one ASC and the other DESC. So separate your two paradigms, let > > MySQL take care of one for you, then manually do the other via PHP. > > > > _______________________________________________ > wp-hackers mailing list > [hidden email] > http://lists.automattic.com/mailman/listinfo/wp-hackers > wp-hackers mailing list [hidden email] http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
> 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 [hidden email] http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
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 <[hidden email]> > > 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 > [hidden email] > 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 [hidden email] _______________________________________________ wp-hackers mailing list [hidden email] http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
On Tue, Apr 5, 2011 at 11:23 AM, Christian Gundersson
<[hidden email]> 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 [hidden email] http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
On Tue, Apr 5, 2011 at 7:28 PM, Otto <[hidden email]> wrote:
> 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' ); > Yeah, except remove_filter() will remove all callbacks attached to posts_orderby, due to this bug: http://core.trac.wordpress.org/ticket/9968 -- http://scribu.net _______________________________________________ wp-hackers mailing list [hidden email] http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
On Tue, Apr 5, 2011 at 12:33 PM, scribu <[hidden email]> wrote:
>> add_filter( 'posts_orderby', 'my_order' ); >> $blah = new WP_Query(...); >> remove_filter( 'posts_orderby', 'my_order' ); > > > Yeah, except remove_filter() will remove all callbacks attached to > posts_orderby, due to this bug: > > http://core.trac.wordpress.org/ticket/9968 Bug doesn't apply. The remove_filter() function, in this case, works and does what you would expect it to do. It doesn't remove all callbacks. This code works perfectly, for example: function run_once() { return 'test'; } add_filter( 'foobar', 'run_once' ); function another_filter() { return 'test again'; } add_filter( 'foobar', 'another_filter', 11 ); remove_filter( 'foobar', 'run_once' ); echo apply_filters( 'foobar', '' ); // produces "test again" -Otto _______________________________________________ wp-hackers mailing list [hidden email] http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
What happens when you call 'remove_filter' from inside the run_once
function and call the filter twice? On Apr 5, 2011 2:35pm, Otto <[hidden email]> wrote: > On Tue, Apr 5, 2011 at 12:33 PM, scribu [hidden email]> wrote: > >> add_filter( 'posts_orderby', 'my_order' ); > >> $blah = new WP_Query(...); > >> remove_filter( 'posts_orderby', 'my_order' ); > > > > > > Yeah, except remove_filter() will remove all callbacks attached to > > posts_orderby, due to this bug: > > > > http://core.trac.wordpress.org/ticket/9968 > Bug doesn't apply. The remove_filter() function, in this case, works > and does what you would expect it to do. It doesn't remove all > callbacks. > This code works perfectly, for example: > function run_once() { > return 'test'; > } > add_filter( 'foobar', 'run_once' ); > function another_filter() { > return 'test again'; > } > add_filter( 'foobar', 'another_filter', 11 ); > remove_filter( 'foobar', 'run_once' ); > echo apply_filters( 'foobar', '' ); // produces "test again" > -Otto > _______________________________________________ > wp-hackers mailing list > [hidden email] > http://lists.automattic.com/mailman/listinfo/wp-hackers _______________________________________________ wp-hackers mailing list [hidden email] http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
Never mind. I get it now. :)
On Apr 5, 2011 3:08pm, [hidden email] wrote: > What happens when you call 'remove_filter' from inside the run_once > function and call the filter twice? > On Apr 5, 2011 2:35pm, Otto [hidden email]> wrote: > > On Tue, Apr 5, 2011 at 12:33 PM, scribu [hidden email]> wrote: > > > > >> add_filter( 'posts_orderby', 'my_order' ); > > > > >> $blah = new WP_Query(...); > > > > >> remove_filter( 'posts_orderby', 'my_order' ); > > > > > > > > > > > > > > > Yeah, except remove_filter() will remove all callbacks attached to > > > > > posts_orderby, due to this bug: > > > > > > > > > > http://core.trac.wordpress.org/ticket/9968 > > > > > > > > > > > > Bug doesn't apply. The remove_filter() function, in this case, works > > > > and does what you would expect it to do. It doesn't remove all > > > > callbacks. > > > > > > > > This code works perfectly, for example: > > > > > > > > function run_once() { > > > > return 'test'; > > > > } > > > > add_filter( 'foobar', 'run_once' ); > > > > > > > > function another_filter() { > > > > return 'test again'; > > > > } > > > > add_filter( 'foobar', 'another_filter', 11 ); > > > > > > > > remove_filter( 'foobar', 'run_once' ); > > > > > > > > echo apply_filters( 'foobar', '' ); // produces "test again" > > > > > > > > -Otto > > > > _______________________________________________ > > > > wp-hackers mailing list > > > > [hidden email] > > > > http://lists.automattic.com/mailman/listinfo/wp-hackers > > > > wp-hackers mailing list [hidden email] http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
In reply to this post by Otto-19
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 <[hidden email]> > On Tue, Apr 5, 2011 at 11:23 AM, Christian Gundersson > <[hidden email]> 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 > [hidden email] > http://lists.automattic.com/mailman/listinfo/wp-hackers > wp-hackers mailing list [hidden email] http://lists.automattic.com/mailman/listinfo/wp-hackers |
| Powered by Nabble | Edit this page |
