この記事は最後に更新してから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_post_type_archive_template(2013年10月29日 更新)
string get_post_type_archive_template()
投稿タイプアーカイブのテンプレートを取得する。
get_comments(2018年5月27日 更新)
array get_comments( [ mixed $args = '' ] )
コメント情報を取得する。
wp_delete_file_from_directory(2018年7月6日 更新)
bool wp_delete_file_from_directory( string $file, string $directory )
パス名を確認した後でファイルを削除する。
get_post_mime_type(2014年11月16日 更新)
mixed get_post_mime_type( [ mixed $ID = '' ] )
添付ファイルのMIMEタイプを取得する。
remove_all_shortcodes(2018年5月27日 更新)
void remove_all_shortcodes( )
すべてのショートコードを削除する。