この記事は最後に更新してから1年以上経過しています。
説明
投稿タグの絞り込みは、カテゴリーに似ているけど微妙に違っている。今日は、昨日投稿した「query_posts(WP_Queryクラス)でカテゴリーを絞り込む」の続きとして、投稿タグの絞り込みついてまとめてみる。まずはquery_posts(WP_Queryクラス)の投稿タグに関連するパラメータを再確認。
| パラメータ | データ | 例 |
|---|---|---|
| tag_id | 投稿タグのIDを指定 | 'tag_id=21' |
| tag__in | 投稿タグのIDを配列で指定 | array( 'tag__in'=>array( 21, 23 ) ) |
| tag__not_in | 投稿タグのIDを配列で指定 | array( 'tag__not_in'=>array( 21, 23 ) ) |
| tag__and | 投稿タグのIDを配列で指定 | array( 'tag__and'=>array( 21, 23 ) ) |
| tag | 投稿タグのスラッグを指定(複数指定する場合は「,」または「+」で区切る) | 'tag=abc,def' |
| tag_slug__in | 投稿タグのスラッグを配列で指定 | 'array( 'tag_slug__in'=>array( 'abc', 'def' ) ) |
| tag_slug__and | 投稿タグのスラッグを配列で指定 | 'array( 'tag_slug__and'=>array( 'abc', 'def' ) ) |
tag_id
'tag_id'は投稿タグが指定された投稿情報を絞り込む。指定できる投稿タグのIDは常に1つのみ。query_posts( 'tag_id=21' );
query_posts( array(
'tax_query' => array(
array(
'taxonomy'=>'post_tag',
'terms'=>array( 21 ),
'include_children'=>true,
'field'=>'term_id',
'operator'=>'IN'
),
'relation' => 'AND'
)
)
);
tag__in
'tag__in'は複数の投稿タグのIDを指定し、その何れかの投稿タグが指定された投稿情報を絞り込む。query_posts( array( 'tag__in'=>array( 21, 23 ) );
query_posts( array(
'tax_query' => array(
array(
'taxonomy'=>'post_tag',
'terms'=>array( 21, 23 ),
'include_children'=>true,
'field'=>'term_id',
'operator'=>'IN'
),
'relation' => 'AND'
)
)
);
tag__not_in
'tag__not_in'は複数の投稿タグのIDを指定し、その何れかの投稿タグも指定されていない投稿情報を絞り込む。query_posts( array( 'tag__not_in'=>array( 21, 23 ) );
query_posts( array(
'tax_query' => array(
array(
'taxonomy'=>'post_tag',
'terms'=>array( 21, 23 ),
'include_children'=>true,
'field'=>'term_id',
'operator'=>'NOT IN'
),
'relation' => 'AND'
)
)
);
tag__and
'tag__and'は複数の投稿タグのIDを指定し、そのすべての投稿タグが指定されている投稿情報を絞り込む。query_posts( array( 'tag__and'=>array( 21, 23 ) );
query_posts( array(
'tax_query' => array(
array(
'taxonomy'=>'post_tag',
'terms'=>array( 21, 23 ),
'include_children'=>true,
'field'=>'term_id',
'operator'=>'AND'
),
'relation' => 'AND'
)
)
);
tag_slug__in
これまでの流れでは'tag'の順番になるが、'tag'はパラメータの指定内容によって変化があるので、先に'tag_slug__in'を取り上げる。'tag_slug__in'は'tag__in'と同様に複数の投稿タグを指定し、その何れかが指定された投稿情報を絞り込む。'tag__in'との違いは、IDではなく、スラッグを指定できるところである。query_posts( array( 'tag_slug__in'=>array( 'good', 'bad' ) );
query_posts( array(
'tax_query' => array(
array(
'taxonomy'=>'post_tag',
'terms'=>array( 'good', 'bad' ),
'include_children'=>true,
'field'=>'slug',
'operator'=>'IN'
),
'relation' => 'AND'
)
)
);
tag_slug__and
'tag_slug__and'は'tag__and'のスタッグ版。投稿タグすべてが指定されている投稿情報を絞り込む。query_posts( array( 'tag_slug__and'=>array( 'good', 'bad' ) );
query_posts( array(
'tax_query' => array(
array(
'taxonomy'=>'post_tag',
'terms'=>array( 'good', 'bad' ),
'include_children'=>true,
'field'=>'slug',
'operator'=>'AND'
),
'relation' => 'AND'
)
)
);
tag
'tag'は、'tag_slug__in'や'tag_slug__and'と同様に、投稿タグのスラッグを指定して使用するが、複数の場合でも配列ではなく、スラッグを「,」または「+」で区切って文字列で指定する。この時「,」で区切ると'tag_slug__in'と同様の意味になり、「+」で区切ると'tag_slug__and'と同様の意味になる。つまり
query_posts( 'tag=good,bad' );
query_posts( array( 'tag_slug__in'=>array( 'good', 'bad' ) );
query_posts( 'tag=good+bad' );
query_posts( array( 'tag_slug__and'=>array( 'good', 'bad' ) );
昨日はカテゴリー、今日は投稿タグの絞り込みについて整理したわけだが、場合によってはこれらを組み合わせで使用することがあるだろう。また、標準テーマのTwentyElevenの短冊ウィジェットのように、投稿フォーマットで絞り込むことも可能である。それらについては、次の機会に紹介する。
最終更新 : 2011年09月14日 18:15
関連
お勧め
has_category(2012年1月17日 更新)
bool has_category( [ mixed $category = '' [ , mixed $post = null ] ] )
投稿記事がカテゴリーに属しているか調べる。
comments_template(2018年5月27日 更新)
void comments_template( string $file = '/comments.php' , bool $separate_comments = false )
投稿ページ(post)または単一ページ(page)のコメント情報を取得し、コメント表示・投稿用のテンプレートファイルを読み込んで表示する。
is_taxonomy_hierarchical(2018年5月27日 更新)
bool is_taxonomy_hierarchical( string $taxonomy )
タクソノミーに階層(親子)関係があるか調べる。
send_confirmation_on_profile_email(2018年5月27日 更新)
void send_confirmation_on_profile_email()
メールアドレス変更を確認するメールを送信する。
get_the_modified_time(2023年9月20日 更新)
string | int | false get_the_modified_time( [ string $format = '' [ , int | WP_Post $post = null ] ] )
パラメータ$formatで指定したフォーマットで投稿の更新時刻を取得する。