Well… It’s pretty easy to explain…
Problem explanation
Your code doesn’t count anything – it retrieves posts that matches given query (you query posts that are in category with ID 9). If you provide no posts_per_page
param, then default value is used (the one that is set in Settings). On the other hand, if you set posts_per_page
to -1
, then you’ll get all posts that matches given query. But doing so is very bad idea – if there are thousands of posts, then you’ll retrieve all of them from DB just to count them.
Solution
One way to fix this would be to check found_posts
instead of post_count
. First one is “The total number of posts found matching the current query parameters” and the second one “The number of posts being displayed”.
$args = array(
'post_type' => 'post',
'cat' => 9,
);
$count_query = new WP_Query( $args );
$count = $count_query->found_posts;
if ( $count >= 3) {
// do something
}
Better solution
But… There is much more efficient way to get count of posts in given category…
$category = get_category( 9 ); // <- change the ID of category
if ( $category->category_count >= 3 ) {
// do something
}
Of course you should also check if the given category exists and so on.