1

我正在尝试做的事情:帖子可以分为 A 或 B 类型。我想做一个存档,计算有多少帖子被归类为 A,有多少在 B 中,以及总数。

问题:我的代码算作 A 类的 12 个帖子。B 类算 5。但两个类别都算 12。

为什么?

我的代码:

$posts_a = new WP_Query('cat=5&category__and=30');
$count_a = $posts_a->post_count;
//gives 12

$posts_b = new WP_Query('cat=5&category__and=29');
$count_b = $posts_b->post_count;
//gives 5

$posts_all = new WP_Query('cat=5');
$count_all = $posts_all->post_count;
//gives 12. It should be at least 12+5.

我不想只是总结 A+B。我想知道出了什么问题。

谢谢

4

1 回答 1

3

It doesn't look like you have just two categories, looking at your queries. From what I can, there are three categories, A, B and C.

There are 12 posts in categories A and B, and 5 in categories A and C, from what I can see from your queries and results.

You are also using category__and incorrectly, which may be the cause of the confusion.

Can you tell me what are the two category IDs you are searching for? I will post the queries after you mention them.

Edit: You probably need this:

$posts_a = new WP_Query(array('category__and'=>array(5,30),'posts_per_page'=>-1));
$count_a = $posts_a->post_count;

$posts_b = new WP_Query(array('category__and'=>array(5,29),'posts_per_page'=>-1));
$count_b = $posts_b->post_count;

$posts_all = new WP_Query('cat=5&posts_per_page=-1');
$count_all = $posts_all->post_count;

Also, this is probably an obvious answer and I don't know your category structure, but is 29 a child category of 30?

于 2013-01-04T19:35:25.660 回答