Display Number Of Posts

Have you ever wanted to display the total number of WordPress posts? How about displaying the total number of posts but excluding specific categories? You’ll learn both of these here!

Place the above code where you’d like to display the total number of published posts.

Code Snippet:

// All pages
$total = wp_count_posts()->publish;
echo 'Total Posts: ' . $total;

// Pages with a category
$categories = get_categories('exclude=1,2,3');
foreach($categories as $category) {
	$total += $category->count;
echo 'Total Posts: ' . $total;

Posted in Posts