特定のカテゴリーまたは特定のタグに属する投稿一覧を作る方法。 ANDではなくOR。
tax_queryを使う
get_postsでcategoryとtagを指定すると指定した両方に属する記事しか取得できない。 tax_queryでカテゴリー、タグをそれぞれ別々に指定しrelationをorにする。query_posts() での指定の仕方
query_posts( array(
'tax_query' => array(
array(
'taxonomy'=>'category',
'terms'=>array( 'news','column', 'feature' ),
'field'=>'slug',
'include_children'=>true,
'operator'=>'IN'
),
array(
'taxonomy'=>'post_tag',
'terms'=>array( 'season' ),
'field'=>'slug',
'operator'=>'IN'
),
'relation' => 'OR'
),
'posts_per_page' => 36
)
);
query_posts() 基本的な使い方
// クエリ(メインクエリを改変) query_posts( $args ); // ループ(改変したメインクエリ) if ( have_posts() ) : while ( have_posts() ) : the_post(); the_title(); endwhile; else: // 何も取得されなかった endif; // クエリをリセット wp_reset_query();