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.

After some searching I haven't been able to find a answer to what I'm trying to archive.

I'm trying to set up an archive page with accordion panels that hide/show posts from a given year with links.

So how do I show an entire year of posts, not just a link to a year?

Example:

2012

Post Title

Next Post Title

Another Post Title

==============

2011

Post Title

Next Post Title

Another Post Title

Etc

share|improve this question
add comment

2 Answers 2

The Simple Yearly Archive Plugin does just that.

This code will also do the trick:

<?php
// get years that have posts
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY year DESC" );

foreach ( $years as $year ) {
    // get posts for each year
    $posts_this_year = $wpdb->get_results( "SELECT post_title FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );

    echo '<h2>' . $year->year . '</h2><ul>';
    foreach ( $posts_this_year as $post ) {
        echo '<li>' . $post->post_title . '</li>';
    }
    echo '</ul>';
}
?>  

There may be room to optimize that, but I've tested and it works.

share|improve this answer
add comment

I think there is probably more than one way to do it but if you don't already know about adjusting the query before you do the loop then you will probably need to read up on it, as I can't see how you'd do it without playing with that. Here is a good place to start.

and if you need to reset the query

share|improve this answer
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.