Skip to content

Count all post in a WordPress blog network

Needed this bit of code to keep count of the total number of posts in a network enabled WordPress site. It pulls the list of blogs and the goes through each, gets the post count, and sums it up.

function getTotalPosts() {
	$total = 0;

	$blogs = get_blog_list(0, 'all');

	foreach ($blogList as $blog) {
		$total += $blog['postcount'];

	return $total;

Certainly not real efficient, and doubtful you’d want it to run often on a large network. But it’s working for the moment.

Although it looks like get_blog_list is deprecated and may not work in future versions of WordPress. Guess I’ll have to figure out another solution when they remove it.

Published inBloggingProgramming

One Comment

  1. Maybe the way to do this is with cron.

    The get_blog_list function does this the way I was going to originally and does a COUNT() query on all of the post tables, once for each blog. Works okay on the network I’m using it on since there’s only about a dozen blogs. Could see that with hundreds or thousands it would get ugly though.

    But it’s probably not something that you’d need a to the second count of anyway though. Once or twice a day a cron script that caches the count would probably be enough.

    Or, maybe I’ll try hooking on to the hook that fires when a post is published and just keep a counter in another table.

Leave a Reply

Your email address will not be published. Required fields are marked *