Opened 6 years ago
Closed 3 years ago
#45267 closed defect (bug) (duplicate)
REST API: get_the_excerpt() on empty post_excerpt causes request to fail
Reported by: |
|
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)
This ticket was mentioned in Slack in #core-restapi by desrosj. View the logs.
6 years ago
#5
@
6 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
@
6 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.
Related: #36934