Hudai Hudai - 1 year ago 93
PHP Question

I am getting max category post count = 10, every time

I have some categories and each category have some subcategories.

For example :

Entertainment(slug = 'entertainment')
--- Movies
--- Drama
--- Sports


I want to retrieve the number count of all posts in
entertainment
and its sub categories

But max count i am getting is 10 everytime.When there is total 13 posts its 10 but if less than 10 posts its exact number. it return 8 for 8..but 10 for 13

My code is



function PrinzPostCatCount( $slug ){
// category posts count
$args = array(
'posts_per_page' => -1,
'category_name' => $slug,
);
$post = new WP_Query( $args );
return (int) $post->post_count;
}

Answer Source

Try to pass 'posts_per_page'=>-1 with the query.

i.e.

$post = new WP_Query( array('category_name' => $slug, 'posts_per_page'=>-1) );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download