Support » Plugins » Hacks » Splitting posts by month, but sorting by day

  • Hi,

    This is confusing me a bit, as it seems such an obvious thing to want to do but I can’t see any suggestions / solutions.

    I have a series of posts which I want to break up by month. That’s easy enough, I use

    $event_query = new WP_Query( array( 'monthnum' => $eventmonth, 'post_type' => array( 'people', 'families'), 'order' => 'ASC', 'orderby' => 'date', 'posts_per_page' => -1 ) );
    while ( $event_query->have_posts() ) :
    // Do stuff

    (I’m using this in a shortcode and passing the month number as $eventmonth).

    Trouble is, of course, that returns all the posts sorted by year, when I want them sorted by the day of the month. I want 1 Jan 2013, 1 Jan 2012, 2 Jan 1973, 3 Jan 1999, whereas I get 2 Jan 1973, 3 Jan 1999, 1 Jan 2012, 1 Jan 2013.

    What I can’t work out is how to to sort my query results by the order of the day of the month. I’m guessing I need to break out the day using the_time (or the_date) but beyond that, I’m lost.

    All help gratefully received!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    Not exactly sure how to format it into a WP_Query argument, but part of the query should be ORDER BY DAYOFMONTH(post_date)

    Hi bcworkz, sorry for the long delay in replying.

    I’ll check that out. I’ve not seen it in a WP_Query call in my research (yet) but it sounds better than anything I’ve thought up so far!


    Moderator bcworkz


    That’s the problem, you may not be able to get that with a WP_Query call. You should however, be able to figure out a way to insert it through the ‘posts_orderby’ filter, but then you’ll need to devise a way so it is only applied to your one query and not every query.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Splitting posts by month, but sorting by day’ is closed to new replies.