WordPress.org

Make WordPress Core

Opened 13 months ago

Last modified 11 months 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:
PR Number:

Description

Very much the issue discussed here:

https://github.com/WP-API/WP-API/pull/2837

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
13 months 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.


11 months ago

#5 @earnjam
11 months 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
11 months 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
11 months ago

@earnjam Better to close it as a duplicate instead FWIW

Note: See TracTickets for help on using tickets.