WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have posts (custom post type) on my site. I would like to create an archive page showing the archived posts ordered by years. e.g:

2010 * post 3 comes here * post 2 comes here * post 1 comes here

2011 * post 3 comes here * post 2 comes here * post 1 comes here

with of course link to the post. I thought it's simple, however I couldnt find anything simple on the web. it also needs to be hand coded so I can style it easily.

will appreciate any help Gil

share|improve this question

i found this code snippet a couple of days ago. can´t remember where... copy this snippet in your archive template

                $year_prev = null;
                $months = $wpdb->get_results(   "SELECT DISTINCT MONTH( post_date ) AS month ,
                YEAR( post_date ) AS year,
                COUNT( id ) as post_count FROM $wpdb->posts
                WHERE post_status = 'publish' and post_date <= now( )
                and post_type = 'post'
                GROUP BY month , year
                ORDER BY post_date DESC");
                foreach($months as $month) :
                    $year_current = $month->year;
                    if ($year_current != $year_prev){
                        if ($year_prev != null){?>
                <?php } ?>
                <h3 class="archive"><?php echo $month->year; ?></h3>
                <ul class="archive-list">
                <?php } ?>
                <a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>">
                <span class="archive-month"><?php echo date("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span>
                <span class="archive-count"><?php echo $month->post_count; ?></span>
                <?php $year_prev = $year_current;
                endforeach; ?>
share|improve this answer
Hi Markus First of all, thanks alot! I tried this snippet but it doesn't present links to the posts underneath the current year. it does post the month's instead... I need to simply have a Year and underneath all the posts related to this year. any help? – gil hamer Sep 1 '11 at 9:25

Your Answer


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.