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

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.