特定の日の投稿を1ページにまとめるショートコード
このBlogではWordPressを使って、投資関連やIT関連などテーマごとに投稿を作成し、それらを日ごとにまとめてページを作成しています。
その際に投稿日を指定して、まとめて投稿を表示するページを作成するショートコードを使用しています。
使い方
ショートコードブロックに、次のように記述します。date=”YYYY-MM-DD”で日付を引数として与えます。

コード
以下のコードをfunction.phpに追加します。
add_shortcode('daily_full_content', function($atts) {
// 現在表示している「まとめ記事」自体のIDを取得
$current_post_id = get_the_ID();
$atts = shortcode_atts(array(
'date' => date('Y-m-d'),
), $atts);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'post__not_in' => array($current_post_id), // 自分自身を除外
// 【追加】並び替え設定:タイトル(辞書順)の昇順
'orderby' => 'title',
'order' => 'ASC',
'date_query' => array(
array(
'year' => date('Y', strtotime($atts['date'])),
'month' => date('m', strtotime($atts['date'])),
'day' => date('d', strtotime($atts['date'])),
),
),
);
$query = new WP_Query($args);
if (!$query->have_posts()) return '<p>指定された日付の個別記事は見つかりませんでした。</p>';
$output = '<div class="daily-full-content-wrapper">';
while ($query->have_posts()) {
$query->the_post();
$output .= '<article class="entry-item" style="margin-bottom: 60px; padding-bottom: 40px; border-bottom: 2px solid #000;">';
$output .= '<h2 style="margin-bottom: 20px; font-size: 1.8em;"><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';
// 本文全文を表示
$content = apply_filters('the_content', get_the_content());
$output .= '<div class="entry-content-body">' . $content . '</div>';
$output .= '</article>';
}
$output .= '</div>';
wp_reset_postdata();
return $output;
});
他、動作仕様など
- 指定された日付に発行された投稿で、自分自身以外を並べて表示します
- 表示順序は、タイトルの辞書順です
- このショートコードを使うと、JetpackによるSNSへの自動投稿で記事を俯瞰してみせる画像が真っ白になったり、ショートコード呼び出し記述の表示になってしまうのが課題です
コメントを残す