This was one of the shortcodes we ever shared to count the number of WP posts. For a more robust function, see this article.
To count other post types, see this page . For example, you might count drafts as
$count_posts->draft; and pages as
wp_count_posts() can be used to find the number for post statuses of any post type. This includes attachments or any post type added in the future, either by a plugin or part of the WordPress Core.