theme by pouretrebelle

テンプレートタグ/query posts - WordPress Codex 日本語版

日時引数

指定した日時の間に投稿された投稿を表示。

  • year (int) - 4桁の年(例:2011)
  • monthnum (int) - 月(1〜12)
  • w (int) - 0〜53 で年間の週を指定。MySQL WEEK command Mode=1 を使っている。
  • day (int) - 日(1〜31)
  • hour (int) - 時(0〜23)
  • minute (int) - 分 (0〜60)
  • second (int) - 秒(0〜60)

今日の投稿を表示

$today = getdate();
query_posts( 'year=' . $today["year"] . '&monthnum=' . $today["mon"] . '&day=' . $today["mday"] );

現在の週の投稿のみを表示

$week = date(‘W’); $year = date(‘Y’); query_posts( ‘year=’ . $year . ‘&w=’ . $week );

12月20日の投稿を表示

query_posts( 'monthnum=12&day=20' );

Note: The queries above return posts for a specific date period in history, i.e. “Posts from X year, X month, X day”. They are unable to fetch posts from a timespan relative to the present, so queries like “Posts from the last 30 days” or “Posts from the last year” are not possible with a basic query, and require use of the posts_where filter to be completed. The examples below use the posts_where filter, and should be modifyable for most time-relative queries.

2009年3月15日の投稿を表示

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
	// posts for March 1 to March 15, 2010
	$where .= " AND post_date >= '2010-03-01' AND post_date < '2010-03-16'";
	return $where;
}
add_filter( 'posts_where', 'filter_where' );

query_posts( $query_string );

最近30日間の投稿を表示

<pre>
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
	// posts in the last 30 days
	$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
	return $where;
}
add_filter( 'posts_where', 'filter_where' );

query_posts( $query_string );

30〜60日前の投稿を表示

<pre>
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
	// posts  30 to 60 days old
	$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
	return $where;
}
add_filter( 'posts_where', 'filter_where' );

query_posts( $query_string );

-  テンプレートタグ/query posts - WordPress Codex 日本語版

Quote posted 5ヶ月前
florean theme by pouretrebelle