I already found out how to get the amount of networkwide posts in WPMS. Until now the function was called by a user who opens the website. So it was rebuild every 24 hours. I don't want to let a single user wait for half a minute until the process is done so I started to work with wp_cron. But there seems to be a problem because I can't see the result in the shortcode. It would be great if someone could take a look to the following lines:

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() {
    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)
        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' );

Thanks a lot!

1 Answer 1


It is set_site_transient, not set_site_site_transient.

You should be aware that a single user will still wait that half a minute for this to complete. wp_cron does not run on clock time. It runs when a user visits the site.

If you have the ability to create a system cron job then hook your callback to an AJAX API action, and use the system cron to trigger it. That should give you a true clock-time activation and avoid having to force some unlucky user to wait.


WordPress Cron Schedule the if and else statement
Should I use set_transient or update_option?

  • Thank you! You're right, there has been set_site_site_transient twice. I'm wondering what is the best way to get and cache the posts count. E.g. is there a way to generate it by an external cron?
    – Philipp
    Jul 21, 2013 at 9:39
  • @Philipp : see the edit
    – s_ha_dum
    Jul 21, 2013 at 13:57
  • Thanks again @s_ha_dum. I think this is to tricky for me at the moment. So I'll leave it as it is a tray it for a couple of days.
    – Philipp
    Jul 22, 2013 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.