【WordPress】投稿を年度毎の表示にする

アーカイブページで、投稿を年度毎に表示させたい場合がありますよね。パラメーターから年度を取得して、サブループを利用して年度毎にリストと、投稿を表示させるコードを書きましたのでご紹介します。

関数は掲載用に同じPHPファイルに記載していますが、functions.phpに移動させても問題ありません。

<?php
$param_year    = isset( $_GET['fy'] ) ? $_GET['fy'] : NULL; // パラメータ取得
$paged         = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 ;
$post_type     = get_post_type( $post );
$post_type_url = get_post_type_archive_link( $post_type );

/**
 * 投稿から年のみを抽出
 */
function get_fiscal_year_array( $post_type, $param_year ) {
	$args = array(
		'posts_per_page' => -1,
		'post_type'      => array( $post_type ),
	);
	$query  = new WP_Query( $args );
	$result = array();
	if ( $query->have_posts() ) {
		while ( $query->have_posts() ) {
			$query->the_post();
			array_push( $result, get_the_date( 'Y' ) );
		}
		wp_reset_postdata();
	}
	$result = array_unique( $result ); //リストから重複を削除
	return $result;
}

function get_post_query( $post_type, $param_year, $paged ) {
	//年度計算
	$year_from = date( $param_year . '-04-01' );
	$year_to   = date( $param_year + 1 . '-03-31' );

	$args = array(
		'posts_per_page' => 12,
		'post_type'      => array( $post_type ),
		'paged'          => $paged
	);

	$selected_args =  array(
		'posts_per_page' => 12,
		'post_type'      => array( $post_type ),
		'paged'          => $paged,
		'date_query'     => array(
			array(
				'after'     => $year_from,
				'before'    => $year_to,
				'inclusive' => true,
			)
		)
	);

	if ( $param_year ) { // パラメーターがある場合
		return $selected_args;
	} else { // パラメーターがない場合
		return $args;
	}
}

function add_class_active( $set, $param_year ) {
	if( $set === $param_year ) {
		echo 'is-active';
	}
}

/**
 * サブループページネーション関数
 */
function theme_the_subloop_pager( $query ) {
	$big  = 999999999; // need an unlikely integer.
	$args = array(
		'base'      => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
		'format'    => '/page/%#%/',
		'current'   => max( 1, get_query_var( 'paged' ) ),
		'prev_text' => '«',
		'next_text' => '»',
		'total'     => $query->max_num_pages,
	);
	echo wp_kses_post( paginate_links( $args ) );
}

?>


<ul>
	<li>
		<a class="<?php add_class_active( NULL, $param_year ); ?>" href="<?php echo $post_type_url; ?>">すべて</a>
	</li>
	<?php
	$post_set_years = get_fiscal_year_array( $post_type, $param_year );
	foreach ( $post_set_years as $post_set_year ) :
		?>
		<li>
			<a class="<?php add_class_active( $post_set_year, $param_year ); ?>" href="<?php echo $post_type_url; ?>?fy=<?php echo $post_set_year; ?>"><?php echo $post_set_year; ?>年度</a>
		</li>
	<?php endforeach; ?>
</ul>

<?php
$query = new WP_Query( get_post_query( $post_type, $param_year, $paged ) );
if ( $query->have_posts() ) :
	?>
	<ul>
	<?php
	while ( $query->have_posts() ) :
		$query->the_post();
		?>

		<?php the_title(); ?>


	<?php endwhile; ?>
	</ul>
<?php else : ?>
	<p>該当記事がありません。</p>
<?php endif; ?>
<?php if ( $paged ) : ?>
	<div class="pager">
		<?php theme_the_subloop_pager( $query ); ?>
	</div>
<?php endif; ?>
<?php wp_reset_postdata(); ?>

石井秀幸@WEBデザイナー
横浜WordPress Meetup 主宰/株式会社ノクチ基地 取締役/モンゼンクリエイティブ合同会社 代表
WEB制作に関する技術や役立つ情報を発信していきます。10年にわたるWordPress歴を背景に、幅広いアイデアを形にするお手伝いをしています。