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. Use per_page and _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=2 limits the result.
  • _fields keeps the response small, the title is an object with a rendered key.

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.