WordPress: List All Posts and Group Them by Month & Year

When I need to list all posts in 2011 grouped by month, I’d do something like this in WordPress (pretty straightforward as explained here):

query_posts('monthnum=12&year=2011');
while ( have_posts() ) : the_post();
  echo '<li>';
  the_title();
  echo '</li>';
endwhile; 

Now, how to list all posts and group them by month and year, without knowing how far I should go back? That is, I don’t know which year was the oldest post written in. Technically, I could try to do monthnum=12&year=2010, monthnum=12&year=2009, and so on; but I feel that there must be a better way.

Related posts

Leave a Reply

1 comment

  1. One solution could be to loop through all years:

     for($i=0;$i<10;$i++){
         $y=2011-$id;
         query_posts("monthnum=12&year=$y");
         while ( have_posts() ) : the_post();
            echo '<li>';
            the_title();
            echo '</li>';
         endwhile; 
     }
    

    This may not be an elegant solution, but use it when there is no better one.