Removing Content From WP API Response When Listing Posts

For many reasons you might want to exclude certain fields from WP API response in certain circumstances. In this short post I’ll be showing you how to remove the “content” field from WP API response when visiting /wp/v2/posts route.

Say for performances’ sake we really don’t want the whole post content to show when we listing posts, since we usually just need excerpt in such case. The code snippet is very straightforward as below:

The trick lies in we check the $request parameter to tell if this is a request to list posts (at line 5-8). Many of us WP developers may feel frustrated about we can’t use these handy is_*() functions such as is_single() or is_archive() when we hack around WP API.

The closest thing we can do is by checking the $request and tell from the parameters it gets. In this case we check if the “id” parameter exists, and if it does, we can confidently say it’s a single post API call instead of a listing posts call. Not sure about this? You can check source code in WP core to see that’s how is_single() works.

Now you get the idea what “last but not least” is all about, right? 😉 Next time be sure to check every parameter for a hook so you can really make use of it.

Still here’s a friendly note from the WP API team that changing API response is discouraged so please do so only when you understand the consequences.

If you’re trying to hack around WP API but having no idea on how to do it, just leave a comment or send me a tweet (@1fixdotio), I’ll get back to you as soon as I can. Talk soon!

I’m a senior web developer helping clients build their websites to grow businesses. Currently I’m based in Taipei, Taiwan.

I write things about WordPress, AngularJS and life. Whenever you’d like to find someone to talk about these topics, just get in touch!

11 Comments

Submit a comment
  • Thanks for your post. Maybe also interest for you or readers, a small plugin that can doing the job is public.

  • Thanks for explaining, I am developing an android client for wordpress website using wp-api and jwt plugin. By default, wordpress did not send an email confirmation when the user changes his email from control panel. I was able to solve this issue using the code provided here
    https://wordpress.stackexchange.com/questions/41730/confirmation-required-on-email-change/47878#47878
    However, when I change the email using a post request to the api route /users/id, I didn’t got any confirmation email.
    another problem is that wp-api didn’t has any route for new user registration as the route /users requires authentication for create operation، so I had to make a new route for user creation with no authentication required using register_new_user() and I got the confirmation email when new user registered as a confirmation link but I need something that I can use in my android app like confirmation code for example….any idea how to solve those problems?

    • Hey MUTASEM,

      Sorry for my late reply. It sounds a complicated problem and I might need to have a look at your code snippet to see what I can do. Feel free to contact me via email if you still look for a solution: yoren [at] 1fix.io

  • Hi, great article!

    An endpoint of `https://example.com/wp-json/wp/v2/pages/123` returns a combination of HTML and JSON format such as:

    HTML Content
    { “id” : 123, … }

    There is an excerpt filter in `functions.php` that set the outputted HTML, in this line:

    function my_custom_filter(){
    echo ‘HTML Content’;
    }
    add_filter (‘the_excerpt’, ‘my_custom_filter’ );

    How to prevent such filter from intervening with the JSON response?

Submit a comment

Your email address will not be published. Required fields are marked *