Wordpress标准循环:带参数的附加post计数

Wordpress标准循环:带参数的附加post计数,wordpress,loops,Wordpress,Loops,我正在修改搜索和过滤Wordpress插件的模板文件,以显示额外的帖子数量 该插件使用标准Wordpress循环,通过特定参数查询帖子。可以使用found\u posts统计帖子数量 现在我想显示第二个post计数,它考虑了其他参数,例如post\u status。我必须坚持常规的WP循环,以保持来自插件的查询完好无损 注释行中的类似内容: if ( $query->have_posts() ) { echo $query->found_posts 'posts found';

我正在修改搜索和过滤Wordpress插件的模板文件,以显示额外的帖子数量

该插件使用标准Wordpress循环,通过特定参数查询帖子。可以使用
found\u posts
统计帖子数量

现在我想显示第二个post计数,它考虑了其他参数,例如
post\u status
。我必须坚持常规的WP循环,以保持来自插件的查询完好无损

注释行中的类似内容:

if ( $query->have_posts() ) {

  echo $query->found_posts 'posts found';
  // echo $query->found_posts(array('post_status=>'private') 'private posts found';

  while ($query->have_posts()) {
    $query->the_post();
    the_title();
  }
}
代码运行良好,但注释部分显然不起作用

是否有方法向标准循环添加另一个带有附加参数的
post_count

谢谢


Georg

您不能像这样添加
echo$query->found\u posts(数组('post\u status=>'private')
,但在循环之前,只需使用下面的代码按
status
对帖子进行计数。如果您想按status private对帖子进行计数,请添加下面的代码

$args = array('post_type' => 'your_post_type_name','post_status' => array('publish', 'pending', 'draft', 'future', 'private', 'inherit', 'trash')    
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {

  echo $query->found_posts 'posts found';

  // echo $query->found_posts(array('post_status=>'private') 'private posts found';
  $count_posts = wp_count_posts('post');
  $private_posts = $count_posts->private;
  echo $private_posts. 'private posts found';
  while ($query->have_posts()) {
    $query->the_post();
    the_title();
  }
}
$publish_posts = $count_posts->publish;
$draft_posts = $count_posts->draft;
同样,如果您想显示发布或草稿文章的数量,可以添加以下代码

$args = array('post_type' => 'your_post_type_name','post_status' => array('publish', 'pending', 'draft', 'future', 'private', 'inherit', 'trash')    
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {

  echo $query->found_posts 'posts found';

  // echo $query->found_posts(array('post_status=>'private') 'private posts found';
  $count_posts = wp_count_posts('post');
  $private_posts = $count_posts->private;
  echo $private_posts. 'private posts found';
  while ($query->have_posts()) {
    $query->the_post();
    the_title();
  }
}
$publish_posts = $count_posts->publish;
$draft_posts = $count_posts->draft;

你可以通过状态来计算任何其他帖子的数量。

谢谢@raju_eww,但结果总是0,尽管有一些私人帖子,我已经登录。猜测插件处理查询的方式会干扰计数。你知道如何使用帖子元数据而不是帖子状态吗?例如“meta_key”=>“color”,“meta_value”=>'red'Hello我忘了添加
post
所以请像
$count\u posts=wp\u count\u posts('post')那样添加它;
。也更新了答案也更新了答案,以及您在查询中传递的参数也是很重要的。也进行了测试和工作,也更新了答案。谢谢@raju_eww!但是我仍然收到了一个错误,
$private_posts=$count_posts()->private;
:未捕获错误:函数名必须是字符串