WordPress.org

Make WordPress Core

Ticket #14405: is_multi_author_cached.diff

File is_multi_author_cached.diff, 1.1 KB (added by westi, 8 years ago)

Check for multi author and wp cache the result, clear on any post transition

  • wp-includes/author-template.php

    ### Eclipse Workspace Patch 1.0
    #P trunk.domain
     
    363363        echo $return;
    364364}
    365365
     366/**
     367 * Does this site have more than one author
     368 *
     369 * Checks to see if more than one author has published posts.
     370 *
     371 * @return bool Whether or not we have more than one author
     372 */
     373function is_multi_author() {
     374        global $wpdb;
     375       
     376        if ( ! $author_count = wp_cache_get('author_count', 'posts') ) {
     377                $author_count = $wpdb->get_var("SELECT COUNT( DISTINCT post_author )  FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'");
     378                wp_cache_set('author_count', $author_count, 'posts');
     379        }
     380
     381        return $author_count > 1;
     382}
     383
     384/**
     385 * Helper function to clear the cache for number of authors.
     386 *
     387 * @private
     388 */
     389function __clear_multi_author_cache() {
     390        wp_cache_delete('author_count', 'posts');
     391}
     392add_action('transition_post_status', '__clear_multi_author_cache');
     393
    366394?>