最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

meta query - List all categories and count number of posts within based on custom date

programmeradmin0浏览0评论

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>

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>
Share Improve this question asked Jan 20, 2021 at 13:42 imagIneimagIne 10512 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you just want to count the total number of posts that matched a specific query, then there's no need to set the posts_per_page to -1. Just set it to 1 instead.

And $count_posts->count returns null because the WP_Query class doesn't have a property named $count, i.e. WP_Query::$count does not exist.

WP_Query does have $post_count property, but that is just the number of posts displayed per page, so you would not use it in your case. Instead, what you would use is the $found_posts property which is the total number of posts found for the query.

So instead of $posts_per_cat = $count_posts->count;, use $posts_per_cat = $count_posts->found_posts;.

And you can check all the properties in WP_Query on this page.

发布评论

评论列表(0)

  1. 暂无评论