WP REST API returns blank response if post is too long

I’m working on an iOS app that requires the use of WP Rest API to fetch posts. Everything is working smoothly and I receive posts by ID using this endpoint:

https://example.com/wp-json/wp/v2/posts/12345

Most of the time the API returns the post as expected, but if the article is longer than 1300 words, it will return an empty response. by empty i mean the page will be completely blank, not even 404 status code or empty array [], It returns code 200 which supposedly means everything looks good, but the response is empty.

From my troubleshooting it seems that the reason for the empty response is that the post has too many words. Is there any way for me to update the WP REST API settings? Or do I have to create a new custom endpoint that can handle the retrieval of large posts?

Any help would be much appreciated.

Updates:I installed the REST API Log plugin to see what’s going on. To my surprise, I am getting the correct response in the response body, but for some reason it is not showing up in any browser. Will have to investigate further.

Update 2: Nothing so far. I have a second wordpress installation and decided to try and replicate things there. I imported a 1300+ word long post into the new site and the same thing happened. If you try to fetch posts by id or slug it will just return a blank page…

Small sarcasm. Not sure what the problem is, but the WP REST API is giving me nothing but problems. Not sure why they decided to update the API as v1 worked perfectly fine for me. v2 just gives me problem after problem.

Update 3:decided to write a custom endpoint and see what’s going on. Wrote a function to return only post_content which seems to be the problem. This is the code that I got:

 function get_long_post ( $params ){
    $post = get_post(59101);

    if( empty( $post ) ){
        return null;
    }
    return $post->post_content;
 }  
 // Register the rest route here.

 add_action( 'rest_api_init', function () {
        register_rest_route( 'custom/v1', 'long-post',array(
            'methods'  => 'GET',
            'callback' => 'get_long_post'
        ) ); 
 } );

It simply pulls a post by its ID and returns the content. I hardcoded the ID of the long post in question and to my surprise, the response is empty. Works flawlessly for any other post that isn’t too long.

Update 4: This “question” may be too long with all my updates. But for the poor soul who has to deal with this, I think it’s best to document as much as you can.

I guess I’m getting somewhere because now I’m able to see at least part of the longer post_content if I get a substring of the overall post_content. It seems that any option with more than 7603 characters will not show up in Chrome or other browsers. Here’s the modified function that returns at least part of the content:

     function get_long_post ( $params ){
        $post = get_post(59101);

        if( empty( $post ) ){
            return null;
        }
        //echo "<script>console.log( 'Debug Objects: " . $post->post_title . "' );</script>";
        $output =  apply_filters( 'the_content', $post->post_content );

        return substr($output,0,7603);
}

Don’t ask me why it is 7603 characters. I don’t know why it’s such an odd number. Will have to investigate further.

Update5: Progress has been made! If I use echo instead of return I get the whole post content. At this point, I’m only thinking of getting the REST API V2 function I need to use echo instead of return. It would have to do a bunch of formatting to get it into valid JSON format but at this point it seems like the only solution. Will update this when I have completed tasks written. Ultimately, for my iOS app I only need a few things from the post like post title, author, content, categories, featured image, post ID and tags. Here is the work so far:

 function get_long_post ( $params ){
    $post = get_post(59101);

    if( empty( $post ) ){
        //return null;
    }
    $output =  apply_filters( 'the_content', $post->post_content );
    echo $output; 
    return null;
 }

Leave a Comment