Make WordPress Core

Opened 16 months ago

Last modified 8 months ago

#39992 new defect (bug)

Bug in get_the_content

Reported by: olik9 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.2
Component: Posts, Post Types Keywords: has-patch has-unit-tests
Focuses: Cc:


In get_the_content $post is not checked if it's null, after

$post = get_post();

and then on line:

if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) )

as you see, $post->post_content is queried. Then if $post is null, the program falls.

A solution can be to check:

$post = get_post();
if ( ! $post ) { return ''; }

and then continue.

Thank you for everything! :)

Attachments (4)

39992.patch (399 bytes) - added by 1naveengiri 16 months ago.
Fixed in this patch.
39992.2.patch (398 bytes) - added by 1naveengiri 16 months ago.
39992.3.diff (1.1 KB) - added by birgire 8 months ago.
39992.4.diff (1.1 KB) - added by birgire 8 months ago.
Here we use assertEmpty() instead of assertEquals()

Download all attachments as: .zip

Change History (11)

#1 @SergeyBiryukov
16 months ago

  • Component changed from General to Posts, Post Types

#2 @swissspidy
16 months ago

  • Keywords needs-patch added

16 months ago

Fixed in this patch.

#3 @1naveengiri
16 months ago

  • Keywords has-patch added; needs-patch removed

#4 @1naveengiri
15 months ago

Hi team, any update on above Bug.

#5 @swissspidy
14 months ago

  • Keywords needs-unit-tests added

8 months ago

#6 @birgire
8 months ago

The empty post object check

$post = get_post( $post );
if ( empty( $post ) ) {
    return '';

is for example used in get_the_excerpt().

Surprisingly there's no explicit test for get_the_content().

It's attempting to write a test that's just a single line:

$this->assertEquals( '', get_the_content() );

as there's no global $post object available within that test method.

But I wanted to show the intention explicitly with:

unset( $GLOBALS['post'] );
$this->assertEquals( '', get_the_content() );

and this is the test in 39992.3.diff.

I did consider writing a much more verbose test, with a check if the global post is set, then store it in a temp, do the unset and then restore it afterwards (cleanup). But since there are currently no global post objects flying around in the test class, I skipped that path.

Last edited 8 months ago by birgire (previous) (diff)

#7 @birgire
8 months ago

  • Keywords has-unit-tests added; needs-unit-tests removed

8 months ago

Here we use assertEmpty() instead of assertEquals()

Note: See TracTickets for help on using tickets.