is_multi_author()
Defines whether there is more than one author on the site who has published posts. Conditional tag.
The function checks how many unique authors have published at least one post and returns true if there are more than one such authors.
This is a conditional function (conditional tag) — usually used in theme templates to output additional elements (for example, links to the author) only if this is a multi-author blog.
Caching
The result is cached in transient cache is_multi_author and filtered through the hook is_multi_author before returning.
When new posts or authors are added, the transient may expire — you can manually clear the cache using delete_transient( 'is_multi_author' ).
Uses the global object $wpdb and an SQL query to the posts table, selecting up to two unique authors who have published posts.
You can change the behavior through the filter is_multi_author to override the value based on the developer's logic:
add_filter( 'is_multi_author', 'your_function' );
Hooks from the function
Returns
true|false.
true— more than one author has published posts on the site.false— only one author publishes posts on the site.
Usage
<?php
if( is_multi_author() ){
// more than one author on the site
}
?>
Examples
#1 Clearing the cache after publishing a new post
To reset the result when adding a post:
add_action( 'save_post', 'clear_multi_author_cache' );
function clear_multi_author_cache( $post_id ) {
delete_transient( 'is_multi_author' );
} #2 Display a link to the author's posts only if there is more than one author on the site:
if( is_multi_author() ){
the_author();
} #3 Add body_class specifying that there is more then one author on the site
Adds a class of group-blog in body tag for site with more than one author (there are several posts with different authors).
add_filter( 'body_class', 'wpdocs_body_classes' );
function wpdocs_body_classes( $classes ) {
if ( is_multi_author() ) {
$classes[] = 'group-blog';
}
return $classes;
}
Notes
- Global. wpdb.
$wpdbWordPress database abstraction object.
Changelog
| Since 3.2.0 | Introduced. |
is_multi_author() is multi author code WP 6.9.1
function is_multi_author() {
global $wpdb;
$is_multi_author = get_transient( 'is_multi_author' );
if ( false === $is_multi_author ) {
$rows = (array) $wpdb->get_col( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2" );
$is_multi_author = 1 < count( $rows ) ? 1 : 0;
set_transient( 'is_multi_author', $is_multi_author );
}
/**
* Filters whether the site has more than one author with published posts.
*
* @since 3.2.0
*
* @param bool $is_multi_author Whether $is_multi_author should evaluate as true.
*/
return apply_filters( 'is_multi_author', (bool) $is_multi_author );
}