WP REST API: Modifying the JSON Response

WP REST API: Modifying the JSON Response

The WordPress API includes several endpoints you can use to read information from the database and use the data for different purposes. In this blog post, we will get use of the /wp-json/wp/v2/posts endpoint, which is something like running a WP_Query. It works using the four most common HTTP methods – GET, POST, PUT and DELETE, for which you will need to be authorized to work with.

* Note that you will need to install the WP REST API plugin.

Because we mostly don’t need and use all the information, there is a way to manipulate the JSON response. WP API provides us with a filter calledrest_prepare_posts which we can use for adding and removing fields (data). So, let's jump into some examples.

The WordPress API includes several endpoints you can use to read information from the database and use the data for different purposes. In this blog post we will get use of the /wp-json/wp/v2/posts endpoint, which is something like running a WP_Query. It works using the four most common HTTP methods – GET, POST, PUT and DELETE, for which you will need to be authorized to work with.

* Note that you will need to install the WP REST API plugin.

Because we mostly don’t need and use all the information, there is a way to manipulate the JSON response. WP API provides us with a filter calledrest_prepare_posts which we can use for adding and removing fields (data). So, lets jump into some examples.

Adding fields

 

Because not all of the post’s data that we may need is included in the JSON response, for example the custom meta fields or URL of the featured image (the ID of the featured image is included), we can easily add them using the rest_prepare_posts filter.

Including Custom Meta Field

Including Featured Image URL

Removing fields

Since we have the featured image URL and we don’t need the featured image ID, there is no need to have it in the JSON response. We can easily remove it using the PHP unset function (line 10).

Modifying the response

Now, since we’ve learned how to add and remove fields from the JSON response, one small example for the end will be how to include only specific fields (data) in the response, without doing unset and other stuff. Lets assume that we will need only the id, title and the link of the post. To avoid doing unset on every other field, we can get them this way:

If you want to read more about modifying the response, there is a full explanation in the documentation.

Share this article

Stay updated with HASELT by signing up for our newsletter

Don't worry, we hate spam too!

Keep reading

HASELT list of 10 book recommendations about software development

HASELT list of 10 book recommendations about software development

Here is a list of 10 book suggestions, shared in the past few months under the hashtag #bookrecommendation, now we are making a list of 10 books about…

Read more
Ripples of Curiosity

Ripples of Curiosity

Once again, we got to see the power, diversity and ripples of human thought. We saw its probing into the microscopic intricate structure of human DNA,…

Read more