[wp-trac] [WordPress Trac] #45267: REST API: get_the_excerpt() on empty post_excerpt causes request to fail

WordPress Trac noreply at wordpress.org
Fri Nov 2 21:30:14 UTC 2018


#45267: REST API: get_the_excerpt() on empty post_excerpt causes request to fail
--------------------------+-----------------------------
 Reporter:  rogerlos      |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  REST API      |    Version:  4.9.8
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 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 ),
     ];
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/45267>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list