Blog / WordPress/ Use WordPress's is_multi_author() Function with Caution

Use WordPress's is_multi_author() Function with Caution

WordPress 的 is_multi_author() 方法应谨慎使用

What is is_multi_author()?

In WordPress development, is_multi_author() is a conditional tag function used to determine if the current site has multiple authors (i.e., more than one user with the 'Author' role or higher). It is commonly used in themes or plugins to decide whether to display multi-author related elements, such as author lists or author bios.

How the Function Works and Potential Issues

The core logic of is_multi_author() queries the database for the count of users with the 'Author' role. However, this query-based approach is precisely why it should be used with caution.

1. Performance Overhead

Each call to is_multi_author() may execute a database query (unless the result is cached by the object cache). During page load, especially within loops or in areas like sidebars where it might be called multiple times, this can add unnecessary database load and impact site performance.

2. Caching Issues

While WordPress attempts to cache the query result, in certain scenarios (such as when using an external object cache or when user roles change frequently) the cache may be invalidated, leading to direct queries on every call.

3. The 'Author' Definition May Not Match Expectations

The function checks for users with the 'Author' role. If your site's content is primarily created by users with the 'Editor' or 'Administrator' roles, and there are few or no 'Author' role users, is_multi_author() may return false even though multiple people are creating content. This can lead to incorrect logical decisions.

Best Practices and Alternatives

Given these issues, consider the following principles when using is_multi_author():

Principle 1: Avoid Use in Loops or High-Frequency Locations

Do not call this function directly within post loops, widgets, or hooks that execute on every page load. If you must use it, store the result in a variable for reuse.

// Not recommended: calling each time in the loop
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        if ( is_multi_author() ) {
            // Display author info
        }
    }
}

// Recommended: get result once to avoid repeated queries
$site_is_multi_author = is_multi_author();
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        if ( $site_is_multi_author ) {
            // Display author info
        }
    }
}

Principle 2: Consider Caching with Transients

For sites where author changes are infrequent, you can use the WordPress Transients API to cache the result for longer periods, such as 12 hours or more.

function get_cached_is_multi_author() {
    $cached_result = get_transient( 'cached_is_multi_author' );
    if ( false === $cached_result ) {
        // Perform the query once and cache the result
        $cached_result = is_multi_author();
        set_transient( 'cached_is_multi_author', $cached_result, 12 * HOUR_IN_SECONDS );
    }
    return $cached_result;
}
// Usage
if ( get_cached_is_multi_author() ) {
    // Your code
}

Note: When a user's role changes (e.g., a new author is added), you should delete this transient to update the cache using delete_transient( 'cached_is_multi_author' ).

Principle 3: Clarify Your Business Logic

Think carefully: do you need to check for 'multiple authors' or for 'content created by multiple people'? If it's the latter, you might need to check multiple roles with publish permissions (like Author, Editor, Administrator) or directly query the count of distinct authors who have published posts. This can be implemented with a custom query, which is more complex but more accurate.

// Example: Check if more than one user has published posts
function is_site_multi_contributor() {
    global $wpdb;
    $author_count = $wpdb->get_var(
        "SELECT COUNT(DISTINCT post_author) FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish'"
    );
    return ( $author_count > 1 );
}

Conclusion

is_multi_author() is a convenient function, but its underlying database query and logic based on the 'Author' role introduce performance and accuracy risks. In development, evaluate its use case, prioritize performance optimization (like result reuse and caching), and ensure its logic aligns with your actual needs. For high-performance or large multi-author sites, implementing a custom, cache-friendly check function is often a more reliable choice.

Post a Comment

Your email will not be published. Required fields are marked with *.