From 69054687be56e6928705b2aa3b136c4423068eae Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 7 Sep 2019 14:32:23 +0000 Subject: [PATCH] Posts, Post Types: Avoid a PHP warning when `the_content()` is called outside of the loop. Fixes #47824. git-svn-id: https://develop.svn.wordpress.org/trunk@46079 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post-template.php | 2 +- tests/phpunit/tests/post/getTheContent.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index ddb5180117..a5ab249948 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -282,7 +282,7 @@ function get_the_content( $more_link_text = null, $strip_teaser = false, $post = return ''; } - if ( null === $post ) { + if ( null === $post && in_the_loop() ) { $elements = compact( 'page', 'more', 'preview', 'pages', 'multipage' ); } else { $elements = generate_postdata( $_post ); diff --git a/tests/phpunit/tests/post/getTheContent.php b/tests/phpunit/tests/post/getTheContent.php index 7bb14a1f93..54b8fe318e 100644 --- a/tests/phpunit/tests/post/getTheContent.php +++ b/tests/phpunit/tests/post/getTheContent.php @@ -75,4 +75,13 @@ class Tests_Post_GetTheContent extends WP_UnitTestCase { $this->assertSame( 'Bang', $found ); } + + /** + * @ticket 47824 + */ + public function test_should_fall_back_to_post_global_outside_of_the_loop() { + $GLOBALS['post'] = self::factory()->post->create( array( 'post_content' => 'Foo' ) ); + + $this->assertSame( 'Foo', get_the_content() ); + } }