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!

12 responses

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

    1. Yoren Chang Avatar
      Yoren Chang

      Hey Frank, did you mean this awesome plugin made by you? https://github.com/bueltge/WP-REST-API-Filter-Items 😉

      1. Yes, I mean this. Maybe the link was stolen in my comment 😉
        Thanks!

        1. Yoren Chang Avatar
          Yoren Chang

          Great! Will definitely give it a go and see how it can fit in my tutorials!

    2. Sandeep Avatar
      Sandeep

      Hi Frank

      Thanks for the plugin. I have installed it but not sure where and how can I use it?
      I want to remove some of the fields returned by my API
      https://mysite.com/wp-json/wp/v2/jobs?filter%5Bmeta_key%5D=jobs&filter%5Bmeta_value%5D=doctor
      It returns all the values I am also using these plugins:
      WP REST API Controller
      WP REST Filter
      Toolset Types to create CPT & Custom Fields.
      Thanks
      Sandeep

  2. Oh, really fine.Thanks a lot for the time!

  3. Hi, this works for me for the default posts, but how could it work for a Custom Post Type?

    1. Hi Juan, for Custom Post Types you need only to do something like this:

      add_filter( ‘rest_prepare_superposts’, ‘my_rest_prepare_post’, 10, 3 );

      using this sintax:
      add_filter( ‘rest_prepare_${type}’, ‘my_rest_prepare_post’, 10, 3 );

      according to this:
      https://developer.wordpress.org/reference/hooks/rest_prepare_this-post_type/

  4. 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?

    1. Yoren Chang Avatar
      Yoren Chang

      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

  5. In which file I need to add above code

  6. Heri Setiawan Avatar
    Heri Setiawan

    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?

Leave a Reply

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