How to display posts month by month?

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?

Related posts

Leave a Reply

3 comments

  1. WordPress 3.7 introduced the date_query to display posts month by month:

    $args = array(
        'date_query' => array(
            array(
                'month' => $month
            )
        )
    );
    $query = new WP_Query( $args );
    

    Note : $month refers to month number (1-12)

  2. 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>nn";
    
            }
    
        ?>
    
            <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');
    
    }
    
            ?>