Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#45267 new defect (bug)

REST API: get_the_excerpt() on empty post_excerpt causes request to fail

Reported by: rogerlos Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.8
Component: Posts, Post Types Keywords: close
Focuses: rest-api Cc:


Very much the issue discussed here:


Summary: If get_the_excerpt is called in an API endpoint on a post which does not have content in post_excerpt, PHP throws a warning:

Trying to get property of non-object in /path/to/wordpress/wp-includes/post-template.php on line 298

This warning is printed before the JSON return and causes the REST promise to fail.

Example endpoint callback which will generate this error if you ask for a post without an excerpt:

function custom_rest_endpoint( $request ) {

    $id = intval( $request->get_param( 'id' ) );
    if ( $id < 1 ) return (object) [ 'error' => 'post id not present' ];

    $post = get_post( $id );

    if ( empty( $post ) ) return (object) [ 'error' => 'post does not exist' ];

    return (object) [
        'id' => $id,
        'title' => $post->post_title,
        'permalink' => get_the_permalink( $id ),
        'excerpt' => get_the_excerpt( $id ),

Change History (7)

#2 @SergeyBiryukov
3 years ago

  • Component changed from REST API to Posts, Post Types
  • Focuses rest-api added

This ticket was mentioned in Slack in #core-restapi by desrosj. View the logs.

3 years ago

#5 @earnjam
3 years ago

We should still get this addressed through the related tickets, but it's no longer an application-breaking bug since we added [43983] silencing PHP warnings in JSON responses.

#6 @earnjam
3 years ago

  • Keywords close added

Per discussion in Slack, adding a close workflow keyword to this to note that it should be resolved/closed by the work on the related tickets and may not require action itself.

#7 @swissspidy
3 years ago

@earnjam Better to close it as a duplicate instead FWIW

Note: See TracTickets for help on using tickets.