WordPress.org

Ready to get started?Download WordPress

Forums

post count multisite (2 posts)

  1. leisegang
    Member
    Posted 1 year ago #

    In functions.php:

    //total site post count
    function post_count() {
    	global $wpdb;
    	if ( false === ( $cached_post_count = get_transient( 'cached_post_count' ) ) ) {
    		// It wasn't there, so regenerate the data and save the transient
    		$cached_post_count = $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->posts . ' WHERE post_status = "publish" AND post_type = "post"');
    		set_transient( 'cached_post_count', $cached_post_count, 60*60*24 );
    	}
    	return $cached_post_count;
    }

    Then display it anywhere in your theme:

    <?php echo get_transient(cached_post_count); ?>

    why isnt this working? it is not outputting anything!

  2. At a guess, this:

    $cached_post_count = $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->posts . ' WHERE post_status = "publish" AND post_type = "post"');

    How does it know what table? You have wp_x_posts :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.