I'd like to create an archive that lists posts under a month header. The months would just be plain text and the post titles, obviously, would link to the posts themselves!


October 2013

  • Post title 1
  • Post title 2

September 2013

  • Post title 1
  • Post title 2

August 2013

  • Post title 1
  • Post title 2

. . . and, if possible, only show one year per page! So, uhhh, I guess it's paginated too.

Is this possible? Probably. I've seen a few threads that deal with similar requests, but nothing quite like this. Or maybe I'm just bad at searching.

Can anyone help me out? Thanks!

1 Answer 1


Here's what I found which I'll break up into pieces:

for ($i = 1; $i <= 12; $i++) {
    $month = date("n", strtotime( date( 'Y-m-01' )." -$i months"));
    $year = date("Y", strtotime( date( 'Y-m-01' )." -$i months"));

The for loop will loop through how many months we want to pull, which in this case is 12. The $month variable will hold an integer without any leading zeros that we can use to reference the current (inside loop current) month later. I'm using strtotime to start at 1st day of the current (Real Time) month and count backwards from there. The same thing will happen with $year

$tmpQuery = new WP_Query(array('monthnum' => $month, 'post-type' => 'post'));

We can then use the $month variable to query all posts in that month. Then we can go into our normal loop.

    for ($i = 1; $i <= 12; $i++) {
        $month = date("n", strtotime( date( 'Y-m-01' )." -$i months"));
        $year = date("Y", strtotime( date( 'Y-m-01' )." -$i months"));

        $tmpQuery = new WP_Query(array('monthnum' => $month, 'year' => $year, 'post-type' => 'post'));

        if($tmpQuery->have_posts()) :
            <h2><?php echo date("F", mktime(0, 0, 0, $month, 10)); ?> - <?php echo $year; ?></h2>

            <?php while($tmpQuery->have_posts()) : $tmpQuery->the_post(); ?>
                <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php endwhile; ?>
        <?php else : ?>
            <p>No Posts This Month</p> 
        <?php endif; 
        // wp_reset_query();
  • certainly there's a more efficient way to do this than running a separate query for every month.
    – Milo
    Oct 31, 2013 at 15:39
  • @Milo That's what I thought, but wp_get_archives only brings month links and he wants posts per month. The only thing I wasn't certain about was paging them by year. Any idea on how to improve this?
    – Howdy_McGee
    Oct 31, 2013 at 16:01
  • you could do just one query per page/year. to organize by month, just check the month of each post and output the month header when it doesn't match the month of the previous post.
    – Milo
    Oct 31, 2013 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.