この記事は最後に更新してから1年以上経過しています。
説明
久しぶりに query_posts関数を見直してみると、見知らぬパラメータがいくつか追加されていた。気になったのでカテゴリー関係の指定を'tax_query'パラメータを交えて整理してみた。query_posts(WP_Queryクラス)でカテゴリーを絞り込むパラメータは次の通り。
パラメータ | データ | 例 |
---|---|---|
category_name | カテゴリーのスラッグを指定(複数指定する場合は「,」で区切る) | 'category_name=abc,def' |
cat | カテゴリーのIDを指定(複数指定する場合は「,」で区切る。除外する場合はIDに「-」を付ける) | 'cat=1,12,-5' |
category__in | カテゴリーのIDを配列で指定 | array( 'category__in'=>array( 1, 12 ) ) |
category__not_in | カテゴリーのIDを配列で指定 | array( 'category__not_in'=>array( 5 ) ) |
category__and | カテゴリーのIDを配列で指定 | array( 'category__and'=>array( 1, 12 ) ) |
category_name
絞り込みを行う上で比較的に扱いやすいのが、スラッグで指定できる'category_name'だろう。例えば'news'と'topics'カテゴリーのどちらかのカテゴリーに属する投稿記事を取得する場合は、次のように記述する。query_posts( 'category_name=news,topics' );
query_posts( array(
'tax_query' => array(
array(
'taxonomy'=>'category',
'terms'=>array( 'news', 'topics' ),
'include_children'=>true,
'field'=>'slug',
'operator'=>'IN'
),
'relation' => 'AND'
)
)
);
category__in
次は'cat'を紹介したいところだが、指定できる値によって若干複雑になってしまうので、先に'category__in'などを紹介する。'category__in'で先と同じように複数のカテゴリーの何れかに属する投稿記事を絞り込む場合は次のように記述する。query_posts( array( 'category__in' => array( 11, 15 ) ) );
query_posts( array(
'tax_query' => array(
array(
'taxonomy'=>'category',
'terms'=>array( 11, 15 ),
'include_children'=>false,
'field'=>'term_id',
'operator'=>'IN'
),
'relation' => 'AND'
)
)
);
また、'include_children'がfalseになるので注意が必要だ。子カテゴリーを含む場合はtrueを指定すること。
category__not_in
この'category__not_in'は'category__in'と反対にカテゴリーに属さない投稿記事を絞り込む。その記述は次の通りだ。query_posts( array( 'category__not_in' => array( 11, 15 ) ) );
query_posts( array(
'tax_query' => array(
array(
'taxonomy'=>'category',
'terms'=>array( 11, 15 ),
'include_children'=>false,
'field'=>'term_id',
'operator'=>'NOT IN'
),
'relation' => 'AND'
)
)
);
category__and
さて個人的にはあまり使ったことはないのだが、複数のカテゴリーに属する投稿記事を絞り込むのが'category__and'になる。query_posts( array( 'category__and' => array( 11, 15 ) ) );
query_posts( array(
'tax_query' => array(
array(
'taxonomy'=>'category',
'terms'=>array( 11, 15 ),
'include_children'=>false,
'field'=>'term_id',
'operator'=>'AND'
),
'relation' => 'AND'
)
)
);
cat
最後に'cat'を紹介する。catの値には、カテゴリーのIDを指定するが、正数の場合ならその数値のIDのカテゴリーを含むものを('category__in'と同じ)、負数の場合はその数値のIDのカテゴリーを含まないものを絞り込む。例えば、カテゴリーIDが11と15のカテゴリーに属し、18のカテゴリーに属さない投稿記事を絞り込む場合は次のように記述する。query_posts( 'cat=11,15,-18' );
query_posts( array(
'tax_query' => array(
array(
'taxonomy'=>'category',
'terms'=>array( 11, 15 ),
'include_children'=>false,
'field'=>'term_id',
'operator'=>'IN'
),
array(
'taxonomy'=>'category',
'terms'=>array( 18 ),
'include_children'=>false,
'field'=>'term_id',
'operator'=>'NOT IN'
),
'relation' => 'AND'
)
)
);
'tax_query'には、カテゴリー以外にも投稿タグや投稿フォーマットをはじめとして、その他のタクソノミーを指定でき、異なるタクソノミーの条件と組み合わせるができる。カテゴリー関連で長くなってしまったので投稿タグやその他のタクソノミーについては次回以上で紹介する。
最終更新 : 2011年09月14日 18:07
関連
お勧め
get_the_posts_navigation(2018年5月27日 更新)
string get_the_posts_navigation( [ array $args = array() ] )
アーカイブページ向けのナビゲーションを取得する。
get_previous_comments_link(2018年5月27日 更新)
string get_get_previous_comments_link( [ string $label = '' ] )
前のコメントリンクを取得する。
is_random_header_image(2011年7月5日 更新)
bool is_random_header_image( [ string $type = 'any' ] )
テーマがランダムヘッダイメージに対応しているか調べる。
term_is_ancestor_of(2019年3月15日 更新)
bool term_is_ancestor_of( int | object $term1, int | object $term2, string $taxonomy )
タームが子孫関係か調べる。
get_custom_logo(2020年9月2日 更新)
string get_custom_logo( [ int $blog_id = 0 ] )
カスタムロゴのHTMLを取得する。