diff --git a/src/wp-includes/author-template.php b/src/wp-includes/author-template.php index b7361a0244..80fd6166ba 100644 --- a/src/wp-includes/author-template.php +++ b/src/wp-includes/author-template.php @@ -206,7 +206,11 @@ function the_author_link() { * @return int The number of posts by the author. */ function get_the_author_posts() { - return count_user_posts( get_post()->post_author ); + $post = get_post(); + if ( ! $post ) { + return 0; + } + return count_user_posts( $post->post_author ); } /** diff --git a/tests/phpunit/tests/user/author.php b/tests/phpunit/tests/user/author.php index 67b9aba756..8646a9ba42 100644 --- a/tests/phpunit/tests/user/author.php +++ b/tests/phpunit/tests/user/author.php @@ -75,4 +75,11 @@ class Tests_User_Author extends WP_UnitTestCase { $this->assertEquals( '', get_the_author_meta( 'user_login' ) ); $this->assertEquals( '', get_the_author_meta( 'does_not_exist' ) ); } -} \ No newline at end of file + + function test_get_the_author_posts() { + // Test with no global post, result should be 0 because no author is found + $this->assertEquals( 0, get_the_author_posts() ); + $GLOBALS['post'] = $this->post_id; + $this->assertEquals( 1, get_the_author_posts() ); + } +}