Get the latest posts with the REST API
Description: Call the REST API of your site and get the two newest posts. Show only the fields id, slug and title.
Hint:
- The route is
/wp-json/wp/v2/posts. Useper_pageand_fields.
Solution
Terminal output
$ curl -s "http://example.com/wp-json/wp/v2/posts?per_page=2&_fields=id,slug,title" | jq -c '.[]'
{"id":7,"slug":"post-three","title":{"rendered":"Post three"}}
{"id":6,"slug":"post-two","title":{"rendered":"Post two"}}
How it works
- The posts come newest first, and
per_page=2limits the result. _fieldskeeps the response small, the title is an object with arenderedkey.
Read REST API Posts.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |