Query multisite and add all totals


I have a multisite for a charity I organize and each chapter has their own site. Everything is working well, but I want to create a function that sums the donation totals from each site.

I am currently using a plugin for donations that add a custom post type to each sites tables (ie wp_#_postmeta ... where # is the site ID). There is a function that does a total for the each individual site, but I want to create a grand total on the main site's home page.

The function I am trying is

global $wpdb;
    $blogs = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public='1'"));
        foreach($blogs as $blog){
            $details = get_blog_details($blog->blog_id);

 // Create the Query
	$posts_per_page = -1;
	$post_type 		= 'donation';
	$post_status 	= 'publish'; 

	$query = new WP_Query( array (
								'post_type'      => $post_type,
								'posts_per_page' => $posts_per_page,
								'post_status'    => $post_status,
								'no_found_rows'  => 1,

	//Get post type count
	$post_count = $query->post_count;
	$i = 1;

	$total_amount = '';

	// Displays info
	if( $post_count > 0) :

		// Loop
		while ($query->have_posts()) : $query->the_post();

			$donation_id     = get_the_ID();
			$donation_amount = get_post_meta($donation_id, 'ewd_amount', true);

			$total_amount 	+= $donation_amount;



	return $total_amount;


But it is only returning the total of he current blog instead of all blogs in the network. Can anyone help with this tweak that I must be missing.


  • Michelle Shull
    • DEV MAN’s Apprentice

    Hi, JR!

    This isn't our Fundraiser plugin, correct? I'm a little limited in what I can do to help you with a directly competing product, but let's see if we can sort this out.

    I believe what's missing here is a tool we use for all of our plugins that aggregate network wide information about posts - Post Indexer. There may be an extra step to tie into it, however. Post Indexer works quietly behind the scenes to create an index of all the posts or custom post types you have on your network. Then you can call that index as needed, which is what we do with our plugins that work this way.

    Does that sound like it might work? If not, we can call in the bigger hammer. :slight_smile:

  • josh_redler
    • Design Lord, Child of Thor

    Hi Michelle,

    Thanks for your quick reply. No this is not your fundraising plugin.

    I looked at post Indexer, and it doesn't do anything without other plugins. But the other plugins that work with it don't do what i'm looking for either, so there would still need to be a custom code to call the post type 'donation' which I can already do, but I want to get it for each site and then add the totals.

    Could you see if the bigger hammer can help tweak my code.


  • Jude
    • DEV MAN

    Hi there @josh

    Sorry the response took a while longer than expected. Appreciate your patience

    Two things

    1) Reset the Loop at the end of each blog


    2) Change the scope of the $total variable to a global, if its something that a lot people will query store it as a transient or a site option and use a cron to update daily


Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.