Independent ASC/DESC in multiple ORDER BY statement.
WP_Query supports ordering results by multiple columns, but does not currently support independent ASC/DESC declarations on those columns. Instead, it concatenates the ORDER and ORDER BY statements together.
In some cases, it would be beneficial to independently set the order for these columns. An example offered on WP-Hackers:
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.
A normal MySql statement would end with ORDER BY author ASC, date DESC.
We should extend WP_Query to allow users to set their ordering parameters independently. This should be done in such a way as to not break backwards compatibility.
I came across the need for this just the other day on two separate sites I was working on. I ended up having to hook into the
posts_orderby filter and modify it for the certain instance I needed - so it was a lot more work than it should have been.
With the more versatile uses of WordPress sites these days, the need arises to perform more complex queries to get the right data. And who wants to have to write their own custom query calls when WP_Query is so great and easy to use?
I mainly just want to agree with extending the WP_Query class to allow for this functionality. With the great features like the meta_queries and the integration of the custom post type taxonomy - it just falls flat without the ability to sort the multiple columns independently.
I was reading your post with the hope for some new information.
The interesting thing here is that I have learned the new element of MySql statement of using ASC & DESC both in the single query, however you were talking about WP.
That was a severe headache for me in my last project. And I have ended it over 2 different queries.
Well, that project was finished but the info gained from you is useful for future project.
Thanks for writing.
Bummer! A patch for this was going to be included in 3.7, but now I see that someone pushed it to a Future Release ¬¬'
RSS feed for this topic
You must log in to post.