我用过这个solution 统计每个类别(列表)的帖子数量。计数基于日期后事件(仅从今天开始)。
我没有得到新WP\\U查询的Var\\u dump show结果。但是$count\\u帖子->;count每个类别返回nUll在我的案例中,至少有一个类别必须显示一篇文章。“是否”;“计数”;功能井显示;0“;如果结果为空或类别不包含帖子?
<ul id="sscat">
<span class="rot90 cwhite" style="transform: rotate(-90deg);">Filtre</span>
<i class="icofont-caret-right icofont-2x cwhite" style="margin-left: -15px;"></i>
<div class="slide_cat w-100 height-auto df fdrow ">
<li class="js-filter-cat item all category_selected" data-category="all">Tous <span class="count-post"></span></li>
<?php
$filter_catactu_args = array(
\'exclude\'=>array(1),
\'option_all\' => \'All\',
\'show_count\' => \'1\'
);
$cats_filter = get_categories($filter_catactu_args);
foreach($cats_filter as $cat_filter) :
$countdatestart = strftime("%Y-%m-%d");
$count_posts_per_category = array(
\'post_type\' => \'post\',
\'posts_per_page\'=> -1,
\'meta_key\' => \'start_dateevents\',
\'cat\' => $cat_filter->cat_ID,
\'orderby\' => \'meta_value\',
\'meta_query\' => array(
array(
\'key\' => \'start_dateevents\',
\'type\'=> \'DATE\',
\'meta_value\' => \'\',
\'value\' => $countdatestart,
\'compare\' => \'>=\'
)
)
);
$count_posts = new WP_Query($count_posts_per_category);
//echo \'<pre>\' , var_dump($count_posts) , \'</pre>\';
$posts_per_cat = $count_posts->count;
echo \'<li class="js-filter-cat item" data-category="\'. $cat_filter->term_id .\'"> \'. $cat_filter->name .\'<span classs="count-post">\'. $posts_per_cat .\'</span> </li>\';
endforeach;
?></div>
</ul>
最合适的回答,由SO网友:Sally CJ 整理而成
如果您只想计算与特定查询匹配的帖子总数,则无需设置posts_per_page
到-1
. 只需将其设置为1
相反
和$count_posts->count
退货null
因为WP_Query
类没有名为的属性$count
, i、 e。WP_Query::$count
不存在。
WP_Query
确实有$post_count
属性,但这只是每页显示的帖子数,因此您不会在您的案例中使用它。相反,您将使用$found_posts
属性,它是为查询找到的帖子总数。
因此$posts_per_cat = $count_posts->count;
, 使用$posts_per_cat = $count_posts->found_posts;
.
您可以在中检查所有属性WP_Query
on this page.