• Hello there,

    As the subject of this post suggests – when running a custom WordPress loop is it possible to ‘orderby’ two different things?

    My WordPress site isn’t used as a blog; each post represents an event.
    When the loop is run, I want these events ordered by date.
    If multiple events run at the same date and time, they should then be ordered alphabetically.

    This is what I have at the moment, which gets all the upcoming events for Sept 9th and spits them out in time order but how do I implement the extra alphabetising?

    query_posts(‘category_name=Programme&posts_per_page=50&orderby=date&order=asc&year=2011&monthnum=9&day=09’);

    I cannot find a solution anywhere online…

    Thank you in advance!

Viewing 8 replies - 1 through 8 (of 8 total)
  • One way to do this is to add a filter to the query_posts. Add the following to your functions.php (but be careful, any errors will make your blog unusable) or to your template before the query:

    function mam_posts_orderby ($orderby) {
       global $mam_global_orderby;
       if ($mam_global_orderby) $orderby = $mam_global_orderby;
       return $orderby;
    }
    add_filter('posts_orderby','mam_posts_orderby');

    then, add these lines around the query:

    $mam_global_orderby = "$wpdb->posts.post_date ASC, UPPER($wpdb->posts.post_title) ASC";
    query_posts('category_name=Programme&posts_per_page=50&orderby=date&order=asc&year=2011&monthnum=9&day=09');
    $mam_global_orderby = ''; // Clear the filter

    Please note that I have not tested the query part, so watch for typos.

    Thread Starter ctzn_erased

    (@ctzn_erased)

    Hi,

    Thanks for your response. I added the code in, and whilst it didn’t break anything it didn’t change anything either.
    My posts are still displaying:

    14:30 – Body and Data
    14.30 – Archives & Whatever
    14.30 – Content and Dissemination

    As all these posts have the same post time, I then want them ordered by title.

    Could this be that WordPress includes seconds in post_date – which are of course uncontrollable via the dashboard? So in terms of seconds, there are no matching dates to then re-order?

    Yes, that is the problem. Try this for the global:

    $mam_global_orderby = "SUBSTR($wpdb->posts.post_date,1,10) ASC, UPPER($wpdb->posts.post_title) ASC";

    Thread Starter ctzn_erased

    (@ctzn_erased)

    Again, thanks for your reply.

    Unfortunately this displays the post ordered by title and completely disregards the post time!

    Sad face.

    My sincere apologies. One more try (tested this time):

    $mam_global_orderby = "DATE_FORMAT($wpdb->posts.post_date,'%Y-%m-%d') ASC, UPPER($wpdb->posts.post_title) ASC";
    Thread Starter ctzn_erased

    (@ctzn_erased)

    This is still just ordering the events by title and disregarding the time.
    Thanks for your help, I’m sorry that I keep coming back and saying things aren’t working!
    I think I’m just going to have to give up on this one…

    It may be a conflict with your theme or a plugin. I tested it on WP 3.2.1, TwentyTen theme and it worked.

    Thread Starter ctzn_erased

    (@ctzn_erased)

    Very interesting.
    I don’t have any plugins in use so it can’t be that, it’s probably the way I’ve shoddily built this theme! How jolly frustrating.
    If that is the case then, sorry for semi-wasting your time but at the same time, thank you for all your help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘wp-query / Can you order by Date AND Title?’ is closed to new replies.