Title: Multisite count posts
Last modified: August 30, 2016

---

# Multisite count posts

 *  Resolved [tcempk](https://wordpress.org/support/users/tcempk/)
 * (@tcempk)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/multisite-count-posts/)
 * Hey there!
 * I’m running a multisite with a main site and 3sub sites (the number of subsites
   will be over 200 in the end). Now on every site I have a category called A, Category
   called B, and Category called C. On my main site I want to publish a count of
   all posts made on all subsites, all posts made on all subsites in category A,
   all posts made on all subsites in category B, all posts made on all subsites 
   in category C. So I get the output like this:
 * All posts: 100
    Posts in category A: 20 Posts in category B: 30 Posts in category
   C: 50
 * Of course a post can be in categories A, B at the same time and can only be counted
   one time.
 * Is there a way to achieve that?

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [swayam.tejwani](https://wordpress.org/support/users/swayamtejwani/)
 * (@swayamtejwani)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/multisite-count-posts/#post-6859437)
 * Hi tcempk,
 * Check these below functions i have created for getting all posts from subsites
   and getting post count from category in all subsites.
 *     ```
       function get_total_posts_in_subsites(){
   
       	$allblogs = wp_get_sites();
   
       	if(count($allblogs) > 0){
   
       		$postscount = 0;
   
       		foreach($allblogs as $blog){
   
       			$blogid = $blog['blog_id'];
   
       			switch_to_blog( $blogid );
   
       			if( !is_main_site( $blogid ) ){
   
       				$thisblog_posts = wp_count_posts();
   
       				$postscount = $postscount + $thisblog_posts->publish;
   
       			} 
   
       			restore_current_blog();
   
       		}
   
       		return $postscount;
   
       		//echo "Number of published posts on all subsites ".$postscount;
   
       	}
   
       }
   
       function count_subsite_posts_from_category( $catname ){
   
       	$allblogs = wp_get_sites();
   
       	if(count($allblogs) > 0){
   
       		$postincat = 0;
   
       		foreach($allblogs as $blog){
   
       			$blogid = $blog['blog_id'];
   
       			switch_to_blog( $blogid );
   
       			if(	! is_main_site( $blogid ) ){
   
       				$args = array(
       						'post_type'		=> 'post',
       						'post_status' 	=> 'publish',
       						'category_name'	=> $catname
       						);
   
       				$query = new WP_Query($args);
   
       				if($query->have_posts()):while($query->have_posts()):$query->the_post();
   
       				$postincat++;
   
       				endwhile;endif;
   
       			} 
   
       			restore_current_blog();
   
       		}
   
       		return $postincat;
   
       	}
   
       }
       ```
   
 * you can put these functions in your functions.php file of theme and in any template
   file you can use these functions by simply calling like
 *     ```
       echo get_total_posts_in_subsites();
       ```
   
 * this will return count of no. of posts in all subsites except main site and the
   other one
 *     ```
       echo count_subsite_posts_from_category('cat A');
       ```
   
 * IN this function count_subsite_posts_from_category, you need to pass category
   name as parameter just like i did ‘cat A’ and this will return “cat A” posts 
   in all subsites, let me know if this helps.
 * Regards,
    Swayam
 *  Thread Starter [tcempk](https://wordpress.org/support/users/tcempk/)
 * (@tcempk)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/multisite-count-posts/#post-6859443)
 * Thanks I’ve figured it out another way just now.
 *     ```
       <?php
   
       global $wpdb;
       $post_count = $wpdb->get_var( "SELECT COUNT(*) FROM wp_2_posts WHERE post_status = 'publish' AND post_type = 'post'  "  );
       $post_count1 = $wpdb->get_var( "SELECT COUNT(*) FROM wp_3_posts WHERE post_status = 'publish' AND post_type = 'post'  "  );
       $post_count2 = $wpdb->get_var( "SELECT COUNT(*) FROM wp_4_posts WHERE post_status = 'publish' AND post_type = 'post'  "  );
   
       $sum = $post_count + $post_count1 + $post_count2;
   
       echo "<p>All posts: {$sum}</p>";
   
       ?>
       ```
   
 * and
 *     ```
       <?php
   
       global $wpdb;
   
       $count_terms = $wpdb->get_var ("SELECT COUNT(*)
       FROM wp_2_term_taxonomy, wp_2_posts, wp_2_term_relationships
       WHERE wp_2_posts.ID = wp_2_term_relationships.object_id
       AND wp_2_term_relationships.term_taxonomy_id = wp_2_term_taxonomy.term_taxonomy_id
       AND wp_2_term_taxonomy.term_id = '22'
       AND wp_2_posts.post_type = 'post'
       AND wp_2_posts.post_status = 'publish' ");
   
       $count_terms1 = $wpdb->get_var ("SELECT COUNT(*)
       FROM wp_3_term_taxonomy, wp_3_posts, wp_3_term_relationships
       WHERE wp_3_posts.ID = wp_3_term_relationships.object_id
       AND wp_3_term_relationships.term_taxonomy_id = wp_3_term_taxonomy.term_taxonomy_id
       AND wp_3_term_taxonomy.term_id = '16'
       AND wp_3_posts.post_type = 'post'
       AND wp_3_posts.post_status = 'publish' ");
   
       $count_terms2 = $wpdb->get_var ("SELECT COUNT(*)
       FROM wp_4_term_taxonomy, wp_4_posts, wp_4_term_relationships
       WHERE wp_4_posts.ID = wp_4_term_relationships.object_id
       AND wp_4_term_relationships.term_taxonomy_id = wp_4_term_taxonomy.term_taxonomy_id
       AND wp_4_term_taxonomy.term_id = '13'
       AND wp_4_posts.post_type = 'post'
       AND wp_4_posts.post_status = 'publish' ");
   
       $sum = $count_terms + $count_terms1 + $count_terms2;
       echo "<p>Posts cat A: {$sum}</p>";
   
       ?>
       ```
   
 * Your aswer seems easier in the long term, so I’ll give it a go if my soultion
   proves to be too much work to update.
 *  Thread Starter [tcempk](https://wordpress.org/support/users/tcempk/)
 * (@tcempk)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/multisite-count-posts/#post-6859444)
 * Your way is much easier! Thank you!
 *  [Anderson Narciso](https://wordpress.org/support/users/andersonnarciso/)
 * (@andersonnarciso)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/multisite-count-posts/#post-6859563)
 * [@tcempk](https://wordpress.org/support/users/tcempk/)
 *     ```
       <?php
   
       global $wpdb;
       $post_count = $wpdb->get_var( "SELECT COUNT(*) FROM wp_2_posts WHERE post_status = 'publish' AND post_type = 'post'  "  );
       $post_count1 = $wpdb->get_var( "SELECT COUNT(*) FROM wp_3_posts WHERE post_status = 'publish' AND post_type = 'post'  "  );
       $post_count2 = $wpdb->get_var( "SELECT COUNT(*) FROM wp_4_posts WHERE post_status = 'publish' AND post_type = 'post'  "  );
   
       $sum = $post_count + $post_count1 + $post_count2;
   
       echo "<p>All posts: {$sum}</p>";
   
       ?>
       ```
   
 * Works, but what if you have 500 blogs with over 1000 posts?
 *  Thread Starter [tcempk](https://wordpress.org/support/users/tcempk/)
 * (@tcempk)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/multisite-count-posts/#post-6859564)
 * Use [@swayam](https://wordpress.org/support/users/swayam/).tejwani solution.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Multisite count posts’ is closed to new replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [count](https://wordpress.org/support/topic-tag/count/)
 * [multisite](https://wordpress.org/support/topic-tag/multisite/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)

 * In: [Networking WordPress](https://wordpress.org/support/forum/multisite/)
 * 5 replies
 * 3 participants
 * Last reply from: [tcempk](https://wordpress.org/support/users/tcempk/)
 * Last activity: [10 years, 2 months ago](https://wordpress.org/support/topic/multisite-count-posts/#post-6859564)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
