こちらのページが大変参考になりました。
http://blog.bitmeister.jp/?p=797
早速コードを公開。
function my_get_archives_by_fiscal_year ( $args = '' ) {
global $wpdb, $wp_locale;
$defaults = array (
'post_type' => 'post',
'limit' => '',
'format' => 'html',
'before' => '',
'after' => '',
'show_post_count' => false,
'echo' => 1
);
$r = wp_parse_args( $args, $defaults );
extract ( $r, EXTR_SKIP );
if ( '' != $limit ) {
$limit = absint( $limit );
$limit = ' LIMIT ' . $limit;
}
$output = '';
$arcresults = (array) $wpdb->get_results(
"SELECT YEAR(ADDDATE(post_date, INTERVAL -3 MONTH)) AS `year`, COUNT(ID) AS `posts`
FROM $wpdb->posts
WHERE post_type = '$post_type' AND post_status = 'publish'
GROUP BY YEAR(ADDDATE(post_date, INTERVAL -3 MONTH))
ORDER BY post_date DESC
$limit"
);
if ( $arcresults ) {
$afterafter = $after;
foreach ( $arcresults as $arcresult ) {
$url = get_year_link( $arcresult->year );
$text = $arcresult->year . '年度';
if ( $show_post_count )
$after = ' (' . $arcresult->posts . ')' . $afterafter;
$output .= get_archives_link( $url, $text, $format, $before, $after );
}
}
if ( $echo )
echo $output;
else
return $output;
}
全体の流れはwp_get_archives()と同じ。
大きな違いは25行目のSQL文。
年の値を「YEAR(ADDDATE(post_date, INTERVAL -3 MONTH))」で3ヶ月分巻き戻して取得しています。これにより2012年3月31日の記事は2011年12月31日となり2011年として処理されます。
取得方法はwp_get_archives()と同じです。
パラメータはtypeを除くwp_get_archives()と同じものすべてと、post_typeが使えます。
ちなみにSQLiteの場合は上記コードの24~31行目を以下と差し替えればOKです。
$arcresults = (array) $wpdb->get_results( ?? ?"SELECT YEAR(datetime(post_date, '-3 month')) AS `year`, COUNT(ID) AS `posts` ?? ?FROM $wpdb->posts ?? ?WHERE post_type = '$post_type' AND post_status = 'publish' ?? ?GROUP BY YEAR(datetime(post_date, '-3 month')) ?? ?ORDER BY post_date DESC ?? ?$limit" );
