Network wide post count (WP Multisite)

You don’t seem to call posts_count_func() anywhere, so if your transient expires, it’s not getting reset.

function posts_shortcode_count_func( $atts ){
    $post_count = get_site_transient( 'total_posts_cache' );
    if( ! $post_count ) {
        posts_count_func();
        $post_count = get_site_transient( 'total_posts_cache' );
    }
    return $post_count;
}
add_shortcode( 'posts', 'posts_shortcode_count_func' );

According to the docs for get_transient() (which is the non-network version of get_site_transient()), false is returned if the transient doesn’t exist or has expired.

Also: You shouldn’t need $args['post_status'] = 'publish'; in your foreach() loop, since you’ve already set that key=>value pair in your $args array up higher.

Leave a Comment