Output yearly archive within a page

You can just use a normal query to grab all posts (which are, by default, sorted date descending), and break them up as you loop over them:

<?php

$posts = new WP_Query(
    array(
        'posts_per_page' => -1,
        'post_type' => 'press-releases'
    )
);

if ( $posts->have_posts() ) : ?>

    <ul class="accordion"><?php

        while ( $posts->have_posts() ) : $posts->the_post();
            $year = get_the_time( 'Y' );

            if ( $posts->current_post === 0 )
                printf( '<li><h3>%s</h3>', $year ); // First post, always open the <li>
            elseif ( $last_year !== $year )
                printf( '</li><li><h3>%s</h3>', $year ); // Unlike above, close the previous open <li>

        ?>

            <div>
                <p><?php the_time( 'j F Y' ) ?></p>
                <a href="https://wordpress.stackexchange.com/questions/144570/<?php the_permalink() ?>"><?php the_title() ?></a>
            </div>

        <?php           

            if ( ( $posts->current_post + 1 ) === $posts->post_count )
                echo '</li>'; // Always close the <li> at the end of the loop

            $last_year = $year;

        endwhile;

    ?></ul>

<?php endif ?>

Leave a Comment