Independent ASC/DESC in multiple ORDER BY statement.

  1. leemon


    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.


    Posted: 4 years ago #
  2. Simon


    It can be usefull, thx

    Posted: 4 years ago #
  3. Don Fischer


    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.

    Don Fischer

    Posted: 4 years ago #
  4. leemon!

    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.

    Posted: 4 years ago #
  5. leemon


    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 ¬¬'


    Posted: 4 years ago #
  6. leemon


    Apparently, someone added a patch for 4.0. I just hope it doesn't get pulled at the last minute.


    Posted: 3 years ago #

RSS feed for this topic


You must log in to post.

  • Rating

    7 Votes
  • Status

    This idea is under consideration