タイトルのとおりです。
あまり需要がないのか調べてもヒットしなくて苦戦しましたがなんとか。
strtotime( ‘-2 years’ ) の部分を-1daysとか-3monthsとか色々変更するとよしです。
<?php
add_filter( 'posts_where', 'filter_where' );
function filter_where( $where = '' ) {
$where .= " AND DATE(post_date) >= '" . date( 'Y-m-d', strtotime( '-2 years' ) ) . "'";
return $where;
}
global $query_string;
query_posts( $query_string );
?>
実際の使い方としてはこんな感じ。
<div class="txt">
<div class="home-c_t">
<ul class="home-tp">
<?php
add_filter( 'posts_where', 'filter_where' );
function filter_where( $where = '' ) {
$where .= " AND DATE(post_date) >= '" . date( 'Y-m-d', strtotime( '-2 years' ) ) . "'";
return $where;
}
global $query_string;
query_posts( $query_string );
?>
<?php query_posts("showposts=-1&cat=3");
while (have_posts()) : the_post();
?>
<?php
$cat = get_the_category();
$catslug = $cat[0]->slug; //スラッグ名
?>
<?php if(empty($post->post_content)) : ?>
<li class="newscontent">
<div class="<?php echo $catslug; ?> date"><?php the_time('Y.m.d');?></div>
<div id="post-<?php the_ID(); ?>"><?php the_title(); ?></div>
</li>
<?php else : ?>
<li class="newscontent">
<div class="<?php echo $catslug; ?> date"><?php the_time('Y.m.d');?></div>
<div id="post-<?php the_ID(); ?>"><a href="<?php the_permalink(); ?>" class="home-a_link"><?php the_title(); ?></a></div>
</li>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
</div>
</div>
参考サイト
http://webcake.no003.info/webdesign/wp-queryposts-datesearch.html
