Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

4.3 Retrieve Data With GET Requests

In this lesson, I'll show you how to retrieve data from the WP REST API and how to pass different arguments for filtering the server response.

4.3 Retrieve Data With GET Requests

Hey folks, and welcome back to the course introducing the WP REST API. I'm instructor Bilal Shaheen. Now that we have learned to explore the API by sending get and options requests. We can put that knowledge to use by retrieving data through different routes. Currently, I'm on the official site of the version 2 of the WP REST API, and here in the left side bar, you can view all the resources supported by the API for performing crude operations. We will begin by working with the POST resource. The route for recurring post in the WP REST API is wpjson/wp/v2/post. And the part of the WordPress installation might like this is localhost/WordPress. This part can an be different in your case. The SUDP in this case should be set to get since we're retrieving data from the server. And after setting these two options, click this blue send button and disable will create posts that will show up here at the bottom. As we can see this data is in JSON format and it's an area of objects with each object representing a particular post. These objects contain different properties like the idea optic post the dated post created the date it was modified, it's title and it's content. In addition to JSON data, the server also to returns a set of headers that can be accessed through the header step. These headers contain a standard header such as the content type, content length did and does she control. In addition to these standard headers, these are were also done some custom headers including the X.WP.Total, and XWP.Totalpages headers. These two headers can come in handy when building pagination. Since these two headers contain the total number of posts the server has and the total number of pages used post the list plan on. The value of the executable pages header depends on the current pagination setting. By default, the server returns a set of ten posts per page, but we can also tell the server to return a set of five posts by setting the per page argument like this. Now the server returns a set of five posts instead of ten, and the value of X-WP-TotalPages header has been changed to eight which was previously four. We can switch between pages by passing the page argument, passing page equals to three will take us to the third page of this post collection. The next thing we need to do is to retrieve posts by their category IDs, we can do so by passing the category's argument. So, if we need to retrieve posts belonging to a category having an ID of three. We can do so by passing the categories equal three argument like this. Since the categories argument is an array, we can pass multiple category ids using the comma separator. [BLANK AUDIO] Or we can also use MDS square brackets to express an area of third degree I.D.s like this. [BLANK AUDIO] And it’s really up to you what matter do you prefer for passing area arguments, adult degree ids like this and it's really up to you what matter do you prefer for passing at area arguments. Likewise you can use other arguments for retrieving posts based on different criteria. You can view all the supported arguments for a particular route using the options request as you saw in the previous lesson. In addition to retrieving a collection of posts, we can retrieve a particular post by mentioning its ID in the route. So in this case, we are retrieving posts with an ID of 10, and this returns a single post object instead of an array. No doubt we have learned to retrieve posts working with other resources distributed as you can just send a similar request to the resources using the supported arguments. So that's all about retrieving data with WP REST API and Postman. In the next lesson, we will learned about creating, updating and deleting data. See you in the next video.

Back to the top