Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How could wp-query be used to show posts month by month, and have it only show the past year? Or is it possible some wp_archive hack could handle this?

share|improve this question
add comment

2 Answers 2

Well it's not a hack it's a function. You can simply use wp_get_archives

http://codex.wordpress.org/Function_Reference/wp_get_archives

Alternatively you can use a wp_query with a time format for actual content, for instance,

//December
$query = new WP_Query( 'monthnum=12' );

http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

share|improve this answer
add comment

Try this, I should clarify that the code is based on a snippet that I saw.

    <?php

$blogtime = date('Y');
$prev_limit_year = $blogtime - 1;
$prev_month = '';
$prev_year = '';

$args = array(
         'posts_per_page' => 20,
         'ignore_sticky_posts' => 1
);

$postsbymonth = new WP_Query($args);

while($postsbymonth->have_posts()) {

    $postsbymonth->the_post();

    if(get_the_time('F') != $prev_month || get_the_time('Y') != $prev_year && get_the_time('Y') == $prev_limit_year) {

                   echo "<h2>".get_the_time('F, Y')."</h2>\n\n";

        }

    ?>

        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

                <?php // your other template tags ?>


    <?php

    $prev_month = get_the_time('F');
    $prev_year = get_the_time('Y');

}

        ?>
share|improve this answer
1  
Can you get this to print out the months with zero posts as well? –  davebowker Aug 5 '12 at 3:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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