WordPress REST API: Get Posts
The posts route /wp-json/wp/v2/posts is the one used most. This chapter shows how to list, filter and page through posts. Let us make some sample content first:
List posts
$ curl -s "http://example.com/wp-json/wp/v2/posts?_fields=id,date,title.rendered" | jq -c '.[]'
{"id":8,"date":"2026-03-04T10:00:00","title":{"rendered":"Site update"}}
{"id":7,"date":"2026-03-03T10:00:00","title":{"rendered":"Learn hooks"}}
{"id":6,"date":"2026-03-02T10:00:00","title":{"rendered":"Learn WP_Query"}}
{"id":5,"date":"2026-03-01T10:00:00","title":{"rendered":"WordPress 7 released"}}
{"id":1,"date":"2026-01-05T10:00:00","title":{"rendered":"Hello world!"}}
The newest post comes first, and only the published posts are shown (the draft is hidden, because we are not logged in). The default is 10 posts per page.
Common parameters
| Parameter | Example | What it does |
|---|---|---|
| per_page | per_page=2 | Number of items (1 to 100) |
| page | page=2 | Page number |
| search | search=hooks | Search in the title and content |
| categories | categories=3 | Filter by category ID (many IDs with comma) |
| tags | tags=4 | Filter by tag ID |
| author | author=1 | Filter by author ID |
| orderby, order | orderby=title&order=asc | Sort the result |
| slug | slug=site-update | Get posts by slug |
| after, before | after=2026-03-03T00:00:00 | Posts after or before a date |
| _fields | _fields=id,title | Return only these fields |
| _embed | _embed | Add the linked data (author, categories, image) in the same response |
Filter, search and sort
$ curl -s "http://example.com/wp-json/wp/v2/posts?categories=3&_fields=id,title.rendered" | jq -c '.[]'
{"id":7,"title":{"rendered":"Learn hooks"}}
{"id":6,"title":{"rendered":"Learn WP_Query"}}
$ curl -s "http://example.com/wp-json/wp/v2/posts?search=update&_fields=id,title.rendered" | jq -c '.[]'
{"id":8,"title":{"rendered":"Site update"}}
$ curl -s "http://example.com/wp-json/wp/v2/posts?orderby=title&order=asc&_fields=id,title.rendered" | jq -c '.[]'
{"id":1,"title":{"rendered":"Hello world!"}}
{"id":7,"title":{"rendered":"Learn hooks"}}
{"id":6,"title":{"rendered":"Learn WP_Query"}}
{"id":8,"title":{"rendered":"Site update"}}
{"id":5,"title":{"rendered":"WordPress 7 released"}}
Pagination
The headers tell the total number of items and pages, so a client can build the page links:
$ curl -s -I "http://example.com/wp-json/wp/v2/posts?per_page=2" | grep -i "^x-wp"
X-WP-Total: 5
X-WP-TotalPages: 3
$ curl -s "http://example.com/wp-json/wp/v2/posts?per_page=2&page=2&_fields=id,title.rendered" | jq -c '.[]'
{"id":6,"title":{"rendered":"Learn WP_Query"}}
{"id":5,"title":{"rendered":"WordPress 7 released"}}
$ curl -s "http://example.com/wp-json/wp/v2/posts?per_page=2&page=9" | jq -c '{code, message}'
{"code":"rest_post_invalid_page_number","message":"The page number requested is larger than the number of pages available."}
Asking for a page which does not exist gives an error with the code rest_post_invalid_page_number.
One post
$ curl -s "http://example.com/wp-json/wp/v2/posts/6?_fields=id,slug,link,categories,content.rendered" | jq -c .
{"id":6,"slug":"learn-wp_query","link":"http://example.com/learn-wp_query/","content":{"rendered":"<p>WP_Query gets posts from the database.</p>\n"},"categories":[3]}
$ curl -s "http://example.com/wp-json/wp/v2/posts/999" | jq -c '{code, message, status: .data.status}'
{"code":"rest_post_invalid_id","message":"Invalid post ID.","status":404}
Get the linked data with _embed
The author, categories and featured image are only IDs in the first response. Use _embed to get the full objects inside the key _embedded, so you need one request, not many:
$ curl -s "http://example.com/wp-json/wp/v2/posts/6?_embed" | jq -c '{title: .title.rendered, author: ._embedded.author[0].name, category: ._embedded["wp:term"][0][0].name}'
{"title":"Learn WP_Query","author":"admin","category":"Tutorials"}
Use it in JavaScript
fetch( '/wp-json/wp/v2/posts?per_page=3&_fields=id,title,link' )
.then( ( response ) => response.json() )
.then( ( posts ) => {
posts.forEach( ( post ) => {
console.log( post.title.rendered, post.link );
} );
} );
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |