WordPress.org

Forums

Splitting posts by month, but sorting by day (4 posts)

  1. YorkshireLad
    Member
    Posted 2 years ago #

    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() ) :
    $event_query->the_post();
    // Do stuff
    endwhile;

    (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!
    Gary

  2. bcworkz
    Member
    Posted 2 years ago #

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

  3. YorkshireLad
    Member
    Posted 2 years ago #

    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!

    Gary

  4. bcworkz
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic