/*-----------------------------------------------------------------------------------------------*/
/* GET MONTHLY ARCHIVE DIVIDED TO YEARS */
/*-----------------------------------------------------------------------------------------------*/
/*
 * splits:
 * <h3>$Year</h3>
 * <ul>
 *		<li><a>$MonthName</a> $Number posts</li>
 * </ul>
 */

function get_by_year($sql, $args) {

	global $wpdb;

	return $sql .= $wpdb->prepare(" AND YEAR(`post_date`) = '%s'", $args['year']);

}

function get_archive_by_year() {

	$archiveString = wp_get_archives('type=yearly&echo=0');

	preg_match_all("#title='(\d{4})'#", $archiveString, $matches);

	foreach ($matches[1] as $year):
		
		echo "<h3>" . $year . "</h3>";
		
		add_filter('getarchives_where', "get_by_year" , 666, 2);
		
		$monthlyArchives = preg_replace('#(.+)(\s\d{4})(</.+>)#', "$1 $3", wp_get_archives("type=monthly&echo=0&show_post_count=1&year=$year")); 
		
		$monthlyArchives = str_replace( '(', '', $monthlyArchives );
		
		$monthlyArchives = str_replace( ')', ' posts', $monthlyArchives );
		
		echo "<ul>" . $monthlyArchives . "</ul>";
		
		remove_filter('getarchives_where', 666);
		
	endforeach;

}