Group archives in archive widget by year & separated by specific month

Currently on my wordpress site, I have archives setup in a widget on the homepage. WordPress automatically separates the archive by month. Rather than doing that, I’d like it to be separated by year, but instead of separating the year at January, I’d like it to be August.

For example, I want it to be separated in this style (it’d be in a dropdown/html select menu):

Read More
2014-2015
 -August 2014
 -September 2014
 ...
 -December 2014
 -January 2015
 ...
 - June 2015
 - July 2015
2013-2014
 -August 2013

I’ve been searching the Internet for what seems like forever and can’t find anything even remotely close.

I think it can be accomplished using wordpress’ mysql, and so far I have this which (mostly) separates it by year:

$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 ) {
    $posts_this_year = $wpdb->get_results( "SELECT ID, post_title FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );
    echo '<select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">';
    echo '<option selected disabled>' . $year->year . '</option>';
    foreach ( $posts_this_year as $post ) {
        echo '<option value="' . get_permalink($post->ID) . '">' . $post->post_title . '</option>';
    }
    echo '</select>';
}

But I can’t get the sql to separate it every August instead of every January.

Any help would be appreciated! 🙂

Related posts

Leave a Reply

1 comment

  1. I can’t test it right now, but this should do the trick:

    ...
    foreach ( $years as $year ) {
        $posts_this_year = $wpdb->get_results("
          SELECT 
            ID, 
            post_title 
          FROM wp_posts 
          WHERE 
            post_type = 'post' 
            AND post_status = 'publish' 
            AND YEAR(post_date - INTERVAL 7 MONTH) = '" . $year->year . "'" 
        );
    ...