年月日を指定して記事を取得する方法
2021年3月13日
date_queryを使った年月日以前、移行それぞれの記事を取得する方法
投稿日が2019年1月1日より以前の投稿の場合(つまり2018年以前の場合)
<?php $args = array(
'date_query' => array(
array(
'before'=>'2019/1/1'
),
),
);
$query = new WP_Query( $args ); ?>
4行目の’before’を’after’に変えることで”以降”になる。
作家さんサイトのArtページで2018年以前の投稿をは一括で出力するで利用。
出力部分は以下
<?php $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> // ここに出力処理 <?php endwhile; endif; ?>
これで2019年以降は「特定カテゴリーの年別投稿一覧(2種類)」のいち番目のコードで年ごとに出力し、22018年以前は上記コードで対応する。
これでうまくいきました。実際のコードが以下。
<?php $args = array(
'post_type' => 'post',//postタイプ
'category_name' => 'artwork',//カテゴリー
'posts_per_page' => -1,//表示件数
'date_query' => array(
array(
'inclusive'=>true,//指定年月日を含める
'before'=>'2018/12/31'
),
),
);
?>
<div class="arts-box-year">
〜2018
</div>
<?php $the_query = new WP_Query( $args ); ?>//ここから通常のサブループ出力
<div class="arts-box">
<div class="panel">
<?php if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="panel-inner">
<a href="<?php the_permalink();?>">
<div class="art-item">
<?php if( has_post_thumbnail() ): ?>
<?php echo get_the_post_thumbnail(); ?>
<?php else: ?>
<img src="<?php echo catch_that_image(); ?>" />
<?php endif; ?>
<div class="mask">
<div class="caption">
<?php the_title();?></div>
</div>
</div>
</a>
</div>
<?php endwhile;
endif;
wp_reset_postdata(); ?>