我正在运行一个多站点,有一个主站点和3个子站点(子站点的数量最终将超过200个)。现在,在每个网站上,我都有一个名为a的类别、一个名为B的类别和一个名为C的类别。在我的主网站上,我想公布所有子网站上发表的所有帖子的数量,所有在a类子网站上发表的帖子,所有在B类子网站上发表的帖子,所有在C类子网站上发表的帖子。因此,我得到如下输出:
所有职位:100个A类职位:20个B类职位:30个C类职位:50个当然,一个职位可以同时处于A类和B类,并且只能计算一次。
我找到了统计所有帖子的方法:
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>";
我对某些类别的帖子有意见。以下是我目前掌握的情况:
global $wpdb;
$wp_count_terms = $wpdb->get_var(
"SELECT COUNT(*) FROM wp_2_term_taxonomy WHERE term_taxonomy_id = \'22\' " );
$wp_count_terms1 = $wpdb->get_var(
"SELECT COUNT(*) FROM wp_3_term_taxonomy WHERE term_taxonomy_id = \'16\' " );
$wp_count_terms2 = $wpdb->get_var(
"SELECT COUNT(*) FROM wp_4_term_taxonomy WHERE term_taxonomy_id = \'13\' " );
$sum = $wp_count_terms + $wp_count_terms1 + $$wp_count_terms2;
echo "<p>Posts in category A: {$sum}</p>";
但它工作不正常。我走对了吗?
SO网友:stillan00b
找到了答案,将代码张贴在下面,以便对后代有所帮助。
<?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>";
?>