例えば「今週の記事一覧」「2014年10月の記事一覧」「2014年11月1日〜11月10日の記事一覧」といった形です。
方法1: WP_Query で日付範囲を指定する
functions.php やテンプレートファイルに以下のようなコードを書きます。
<?php
$args = array(
'post_type' => 'post', // 投稿タイプ
'posts_per_page' => 10, // 表示件数
'date_query' => array(
array(
'after' => '2014-11-01', // 開始日
'before' => '2014-11-10', // 終了日
'inclusive' => true, // 範囲を含める
),
),
'orderby' => 'date',
'order' => 'DESC',
);
$query = new WP_Query($args);
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post(); ?>
<article>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p>公開日: <?php echo get_the_date('Y年n月j日'); ?></p>
<?php the_excerpt(); ?>
</article>
<?php endwhile;
endif;
wp_reset_postdata();
?>
PHP方法2: 月別・週別アーカイブを使う
WordPressには標準で「月別アーカイブ」があります。
wp_get_archives()を使うと月ごとのリンク一覧を生成可能。- 週単位は標準ではないので、
date_queryを使って「今週分」などを取得します。
例: 今週の記事一覧
<?php
$args = array(
'date_query' => array(
array(
'after' => 'monday this week',
'before' => 'sunday this week',
'inclusive' => true,
),
),
);
$query = new WP_Query($args);
PHP方法3: ショートコード化して柔軟に使う
functions.php にショートコードを作っておくと便利です。
function posts_by_date_range($atts) {
$atts = shortcode_atts(array(
'after' => '2014-11-01',
'before' => '2014-11-10',
), $atts);
$args = array(
'post_type' => 'post',
'date_query' => array(
array(
'after' => $atts['after'],
'before' => $atts['before'],
'inclusive' => true,
),
),
);
$query = new WP_Query($args);
$output = '';
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$output .= '<p><a href="'.get_permalink().'">'.get_the_title().'</a></p>';
}
}
wp_reset_postdata();
return $output;
}
add_shortcode('posts_by_date', 'posts_by_date_range');
PHP→ 投稿や固定ページに
[posts_by_date after="2014-11-01" before="2014-11-10"]
PHPと書けば、その期間の記事一覧が表示されます。
✅ まとめ
- 期間指定 →
date_queryを使う - 月別一覧 →
wp_get_archives() - 柔軟に使いたい → ショートコード化


