【问题标题】:Wordpress counting posts wrongWordPress 计数帖子错误
【发布时间】:2012-12-19 06:31:21
【问题描述】:

我正在尝试做的事情: 帖子可以分为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。我想知道哪里出了问题。

谢谢

【问题讨论】:

  • 你的帖子有重叠吗?也就是说,两者都属于?
  • 您的意思是,A 类和 B 类的帖子同时发布?不 :(。谢谢!

标签: wordpress counting


【解决方案1】:

您的查询看起来不像只有两个类别。 据我所知,有A、B、C三类。

从您的查询和结果中可以看出,A 类和 B 类有 12 个帖子,A 类和 C 类有 5 个帖子。

您也错误地使用了 category__and,这可能是造成混淆的原因。

您能告诉我您要搜索的两个类别 ID 是什么吗?我会在你提到它们后发布查询。

编辑:你可能需要这个:

$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;

另外,这可能是一个显而易见的答案,我不知道您的类别结构,但 29 是 30 的子类别吗?

【讨论】:

  • 感谢您的回答。是的,网站可能有很多类别(例如 ID:1、2、3、4、5、6 ...),但所有类别都必须归类为 A(ID 30)或 B(ID 29)。 ID 5 的存档应显示:1) ID 为 5 的帖子也归类为 A (ID 30)。 2) ID 为 5 的帖子也归类为 B (ID 29)。 3) ID 为 5 的所有帖子(应包括归类为 A、B 甚至我忘记归类为 A 或 B 的帖子)。 ID 5 和 A 返回我 12。ID 5 和 B 返回我 5。所有 ID 5 都返回我 12。
  • @Leo 编辑了我的答案。看看吧。
  • 试过这个!它给了我相同的数量(第 1 次为 12,第 2 次为 5)。对我来说没有意义的是总数的计数。
  • 你确定没有重叠的帖子吗?试试$posts_c = new WP_Query(array('category__and'=>array(5,29,30)))
  • 在 WP_QUery 数组中添加posts_per_page 试试? $posts_all = new WP_Query('cat=5&posts_per_page=-1'); 实际上,在所有 WP_Queries 中使用它。看看我编辑的答案
猜你喜欢
  • 1970-01-01
  • 2019-01-29
  • 2017-03-07
  • 1970-01-01
  • 1970-01-01
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多