Sort Posts by Year in Feed

Sometimes it’s necessary to further customize archived data into sortable sections (organized by date, like below, or category), so it’s easier for visitors to find what they’re looking for at-a-glance.

This example of an annual-delimited loop can help differentiate between content over a lengthy period of time:

<?php $args = array(

        'posts_per_page' => -1,

        'post_type' => 'production',

        'post_status' => 'publish'

    );   

    $my_query = null;

    $my_query = new WP_Query($args); 



if( $my_query->have_posts() ) {

        while ($my_query->have_posts()) : $my_query->the_post(); ?>



<?php

    $year = get_the_date('Y', '', '', FALSE);



     if ($year !== $year_check) {

     echo "<h2 class='year'>" . $year . "</h2>";

     }

     $year_check = $year;

?>



   <a href="<?php the_permalink(); ?>"><div class="production_wrap">

		<?php the_post_thumbnail('medium'); ?>

		<h3><?php the_title(); ?></h3>

	</div></a><!-- end .post-wrap -->



<?php endwhile;

    }

wp_reset_query();  ?>

Sourced here.

Leave a Reply

Posted in php
katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.