この記事は最後に更新してから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_image_size(2014年4月24日 更新)
bool has_image_size( string $name )
イメージサイズが登録済みか調べる。
wp_publish_post(2020年12月12日 更新)
void wp_publish_post( int | WP_Post $post )
投稿を公開する。
get_category(2018年5月27日 更新)
mixed get_category( mixed $category [ , string $output = OBJECT [ , string $filter = 'raw' ] ] )
IDを指定してカテゴリー情報を取得する。
add_term_meta(2018年5月27日 更新)
mixed add_term_meta( int $term_id, string $meta_key, mixed $meta_value [ , bool $unique = false ] )
タームのメタ情報を追加する。
win_is_writable(2013年8月3日 更新)
bool win_is_writable( string $path )
Windows環境でディレクトリが書き込みできるか調べる。