Make WordPress Core

Opened 6 years ago

Closed 23 months ago

#45267 closed defect (bug) (duplicate)

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

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

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 (8)

#2 @SergeyBiryukov
6 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.


5 years ago

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

@earnjam Better to close it as a duplicate instead FWIW

#8 @hellofromTonya
23 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

This issue has been resolved:

  • #44534 / [43983] silenced the PHP warnings in JSON responses
  • #36934 / [44941] added a guard clause to verify $post is an instance of WP_Post

As [44941] prevents the PHP warning, marking this ticket as a duplicate of #36934, ie meaning that ticket resolved this issue.

Note: See TracTickets for help on using tickets.