admin管理员组

文章数量:1314349

How to get just the common posts of 2 different categories? consider this category tree:

posttype
-news
-arrangement
--course
--competetion
--other
region
-region1
-region2
-region3

Now I want to get the posts in categories 'news' and 'region2'. I have tried this:

$query = new WP_Query('category__and=12,32'); // 12=news, 32=region2

But then I get all the posts which have either category 12 or category 32. I want those posts which have both 12 and 32.

How to get just the common posts of 2 different categories? consider this category tree:

posttype
-news
-arrangement
--course
--competetion
--other
region
-region1
-region2
-region3

Now I want to get the posts in categories 'news' and 'region2'. I have tried this:

$query = new WP_Query('category__and=12,32'); // 12=news, 32=region2

But then I get all the posts which have either category 12 or category 32. I want those posts which have both 12 and 32.

Share Improve this question asked Oct 27, 2014 at 14:04 Imrul.HImrul.H 1752 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

category__and needs to be an array of category ID's

category__and (array) - use category id.

You can modify your code to look like this

$query = new WP_Query( array('category__and' => array(12,32)));

本文标签: wp queryGet posts with multiple categories