Skip to content

Instantly share code, notes, and snippets.

@whyisjake
Last active August 29, 2015 14:03
Show Gist options
  • Save whyisjake/4932d6b6071a286e9f3f to your computer and use it in GitHub Desktop.
Save whyisjake/4932d6b6071a286e9f3f to your computer and use it in GitHub Desktop.
<?php
// The date to start on.
$current_year = date( 'Y' );
// This doesn't matter that much, but it will
// make the page load a little faster if you
// know exactly what the range is.
$ending_year = 2000;
$years = range( $current_year, $ending_year );
// Let's loop through all the years.
foreach ( $years as $year ) {
// Now, let's loop through all of the months
for ( $i = 12; $i > 0; $i-- ) {
$args = array(
'post_type' => array( 'post', 'recipe', 'event' ),
'posts_per_page' => 1,
'monthnum' => $i,
'year' => $year,
);
$md5 = md5( serialize( $args ) );
$query = wp_cache_get( 'month-' . $md5 );
if( $query == false ) {
// Looks like the cache didn't have our data
// Let's generate the query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<article class="col-sm-3">' . get_the_post_thumbnail() . '</article>';
endwhile;
// Now, let's save the data to the cache
// In this case, we're telling the cache to expire the data after 300 seconds
wp_cache_set( 'month-' . $md5, $the_query, '', 300 );
}
// Restore original Post Data
wp_reset_postdata();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment