Under what circumstances would WordPress REST API return empty content/excerpt?
Asked Answered
L

2

7

I'm running the WordPress REST API (on WordPress 4.7.3) and when I hit /wp-json/wp/v2/posts I'm getting back JSON for my posts, but the content and excerpt fields are showing up blank - like this:

"status": "publish",
"type": "post",
"link": "https://www.example.com/myblog/blah/",
"title": {
    "rendered": "Title goes here"
},
"content": {
    "rendered": "",
    "protected": false
},
"excerpt": {
    "rendered": "",
    "protected": false
},
"author": 192,

Those posts definitely have content! Any ideas what could be causing the content and excerpt not to be returned by the API?

Louielouis answered 13/4, 2017 at 0:19 Comment(6)
Hi Simon. Can you try /wp-json/wp/v2/post/{ID} with the ID of the specific post?Dordrecht
I tried that, exactly the same result - those fields come back as "rendered": ""Louielouis
I guess that your content is getting cutoff by some plugin that is using the_content hook or similar.. can you try to disable any of such plugins?Ozonosphere
It could be a filter somewhere - have you tried turning off all your plugins and switching to a default theme to see if that fixes the problem (on a staging site if possible)? If you're using a content builder that might also explain it - if it puts everything in shortcodes or postmeta then the_content might actually be empty because that's not where the data is stored.Felipe
Figured d it out? Same problem hereMinimize
I'm experiencing the same or a similar issue for the content field, but only for particularly long posts. I saw another WordPress StackExchange post dealing with this (posts that are too long returning empty in the REST response) with what appears to be a partial solution. I have not followed up to determine if the solution works. wordpress.stackexchange.com/questions/317823/…Mutz
G
0

Try to change your URL below like this, this should work, otherwise disable all your installed plug-in and try this URL, maybe plug-in conflict will affect content,

Add extra parameters in URL like this,

http://example.com/wp-json/wp/v2/posts?items=id,title,featured_media
Goto answered 22/4, 2017 at 12:29 Comment(0)
Y
0

I know this old, but I just had the same issue and figured out the cause / fix.

In my case, the excerpt appeared empty, but actually contained a bunch of whitespace characters. Rendering this out with the REST-API call simply resulted in an empty string.

If I went into my post and manually erased all of the whitespace characters in the excerpt, that post started working as expected.

However, I had 225 posts that I didn't want to manually strip whitespace from, so to automatically trim out whitespace in excerpts (and therefore display the auto-generated excerpt if the result was empty), I used the following code in my theme's functions file (from https://mindpalette.com/2019/03/wordpress-excerpts-empty-but-post-has-content/)

// trim excerpt whitespace
if ( !function_exists( 'mp_trim_excerpt_whitespace' ) ) {
  function mp_trim_excerpt_whitespace( $excerpt ) {
    return trim( $excerpt );
  }
  add_filter( 'get_the_excerpt', 'mp_trim_excerpt_whitespace', 1 );
}
Ylla answered 14/4, 2020 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.