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
(@tcempk)
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
(@tcempk)
Your way is much easier! Thank you!
@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
(@tcempk)
Use @swayam.tejwani solution.