How to get a post count on a WordPress site

The wp_count_posts() function will return an object containing a count for each status type.

$count = wp_count_posts();

The status types are publish, future, draft, pending, private, trash, auto-draft and inherit.

echo $count->publish; // 82
echo $count->future; // 3
echo $count->draft; // 16
echo $count->pending; // 1
echo $count->private; // 0
echo $count->trash; // 25
echo $count->auto-draft; // 1
echo $count->inherit; // 0

Note that by default the count applies to posts. You can pass the function a type to get a count for that type.

$count = wp_count_posts( 'page' );