Skip to content

Instantly share code, notes, and snippets.

@yhira
Last active April 20, 2018 02:47
Show Gist options
  • Save yhira/ffd5905170459f8d0668bed368dfb116 to your computer and use it in GitHub Desktop.
Save yhira/ffd5905170459f8d0668bed368dfb116 to your computer and use it in GitHub Desktop.
query_postsでカテゴリ指定する際に子カテゴリーも含めた結果を取得する
<?php
$args = array(
'posts_per_page' => 10, //取得する結果数
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => array(111, 222, 333), //カテゴリーIDを指定する
'include_children' => true,
'field' => 'term_id',
'operator' => 'IN'
),
'relation' => 'AND'
)
);
query_posts( $args ); //クエリの作成
if ( have_posts() ) : while ( have_posts() ) : the_post();
echo '<p>コンテンツの内容を書く</p>';
endwhile;
else :
echo '<p>Not Found</p>';//見つからない時のメッセージ
endif;
wp_reset_query();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment