I already found out how to get the amount of networkwide posts 在WPMS中。到目前为止,该函数是由打开网站的用户调用的。所以每24小时重建一次。我不想让一个用户等待半分钟,直到过程完成,所以我开始使用wp\\u cron。但似乎有一个问题,因为我无法在短代码中看到结果。如果有人能看看以下几行,那就太好了:
register_activation_hook(__FILE__, \'wpms_stats_activation\');
add_action(\'wpms_stats_daily_event\', \'posts_count_func\');
register_deactivation_hook(__FILE__, \'wpms_stats_deactivation\');
// Start the cron on activation
function wpms_stats_activation() {
wp_schedule_event(time(), \'daily\', \'wpms_stats_daily_event\');
}
// Delete the cache and deactivate the cron
function my_deactivation() {
wp_clear_scheduled_hook(\'wpms_stats_daily_event\');
delete_site_transient( \'total_posts_cache\' );
}
// Count the posts of the whole network
function posts_count_func( $args ){
global $wpdb;
$blogs = $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM {$wpdb->blogs} WHERE spam = \'0\'
AND deleted = \'0\' AND archived = \'0\'
ORDER BY registered DESC, 2", ARRAY_A ) );
$original_blog_id = get_current_blog_id();
$args = array(
\'numberposts\' => -1,
\'post_type\' => \'post\',
\'post_status\' => \'publish\' );
$total_network = $draft_network = 0;
$total_sites = 0;
foreach ($blogs as $blog)
{
wp_cache_flush();
switch_to_blog( $blog->blog_id );
$args[\'post_status\'] = \'publish\';
if (count(get_posts($args))<2) { continue; }
$total_posts = count( get_posts( $args ) );
$total_network += $total_posts;
$total_sites += 1;
$args[\'post_status\'] = \'draft\';
}
set_site_site_transient ( \'total_posts_cache\', $total_network );
switch_to_blog( $original_blog_id );
}
//Make a shortcode which shows the amount of posts
function posts_shortcode_count_func( $atts ){
$post_count = get_site_transient( \'total_posts_cache\' );
return $post_count;
}
add_shortcode( \'posts\', \'posts_shortcode_count_func\' );
非常感谢!