0

I'm trying to show the total amount of posts of my WordPress Multisite. For that I'm using the following code, which worked at the beginning. So I guess that there's something wrong with the set_site_transient which should cache the output for a time.

Maybe someone has an idea what my mistake is. I can't find the solution.

Cheers and thanks a lot!

    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_transient ( 'total_posts_cache', $total_network, 24 * HOUR_IN_SECONDS );



 switch_to_blog( $original_blog_id );
}

function posts_shortcode_count_func( $atts ){
 return get_site_transient( 'total_posts_cache' );
}
add_shortcode( 'posts', 'posts_shortcode_count_func' );
1

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.

| improve this answer | |
  • Ahhh! Yes! Thank you so much! Should I use get_transient() instead of get_site_transient()? – Philipp Jul 13 '13 at 10:29
  • No, for your case, use get_site_transient() -- I linked to get_transient() because the page for get_site_transient() essentially says "See get_transient() for details". – Pat J Jul 13 '13 at 15:51
  • @ Pat J: Thank you very much. I'm still struggeling and opened a new question. If it's possible to you it would be great if you could take another look here: wordpress.stackexchange.com/questions/107244/… – Philipp Jul 20 '13 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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