Multisite posts in categories on network

Found the answer, posting the code below so it may help future generations.

<?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>Category A sum: {$sum}</p>";

?>

Leave a Comment