How to get count of posts assigned to given category?

I have the following code to count the number of posts in a specific category. Based on the number of posts that it returns it should do something.

$args = array(
‘posts_per_page’ => -1,
‘post_type’ => ‘post’,
‘cat’ => ‘9’,
);

$count_query = new WP_Query( $args );
$count = $count_query->post_count;

if ( $count >= 3) {

// do something

}

This code works but only if I include posts_per_page in my array. Can someone explain me why this is?

I expected the code to work without the post_per_page as well. Instead it returns a 0 as result.

Read more here:: How to get count of posts assigned to given category?

Leave a Reply

Your email address will not be published. Required fields are marked *