diff --git a/src/wp-includes/author-template.php b/src/wp-includes/author-template.php index 2570a4035b..faeb6a89a0 100644 --- a/src/wp-includes/author-template.php +++ b/src/wp-includes/author-template.php @@ -481,14 +481,15 @@ function wp_list_authors( $args = '' ) { $post_counts = apply_filters( 'pre_wp_list_authors_post_counts_query', false, $parsed_args ); if ( ! is_array( $post_counts ) ) { - $post_counts = $wpdb->get_results( + $post_counts = array(); + $post_counts_query = $wpdb->get_results( "SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE " . get_private_posts_cap_sql( 'post' ) . ' GROUP BY post_author' ); - foreach ( (array) $post_counts as $row ) { + foreach ( (array) $post_counts_query as $row ) { $post_counts[ $row->post_author ] = $row->count; } } diff --git a/tests/phpunit/tests/user/wpListAuthors.php b/tests/phpunit/tests/user/wpListAuthors.php index ebf557157a..90a22e39d9 100644 --- a/tests/phpunit/tests/user/wpListAuthors.php +++ b/tests/phpunit/tests/user/wpListAuthors.php @@ -146,6 +146,31 @@ class Tests_User_wpListAuthors extends WP_UnitTestCase { ); } + /** + * Ensures the 'optioncount' parameter does not throw an error when there are authors without posts. + * + * @ticket 57011 + */ + public function test_wp_list_authors_optioncount_should_not_error_for_empty_authors() { + /* + * The main purpose of this test is to ensure that the error below is not thrown: + * + * Error: Object of class stdClass could not be converted to string + * + * In place of direct testing we ensure `wp_list_authors()` returns a list of authors + * at least one of which is empty. + */ + $actual = wp_list_authors( + array( + 'optioncount' => true, + 'hide_empty' => false, + 'exclude_admin' => false, + 'echo' => false, + ) + ); + $this->assertStringContainsString( '(0)', $actual ); + } + public function test_wp_list_authors_exclude_admin() { self::factory()->post->create( array(