I used this solution to count number of posts for each category (list). The counting is based on date post event (only from today's date).
I got nothing var_dump show result of new WP_Query . but $count_posts->count return nUll for each category at least one category must display one post in my case. does "count" function well display "0" if result is null or category doesn't contain posts ?
<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>