delarge
11/5/2015 - 4:13 PM

Get yearly archive links for custom post type in Wordpress

Get yearly archive links for custom post type in Wordpress

<div class="side-nav">

  <ul class="side-archives">

    <?php
    $cpt = 'research';

    // Get years
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = '$cpt' ORDER BY post_date DESC");
    foreach($years as $year) :
    ?>

            <li>
                <a href="<?php echo get_post_type_archive_link( $cpt ); ?>?y=<?php echo $year; ?>">
                    <?php echo $year; ?>
                </a>
            </li>

    <?php endforeach;?>

  </ul>

</div>
<!-- side-nav -->