A WordPress REST API request can return valid JSON and still leave you with an incomplete export. Collection endpoints are paginated: the first response is only one slice. A script that assumes one request contains every post may quietly miss most of the site.
The reliable approach is to request a reasonable page size, inspect the pagination headers, and continue until the final page. This guide shows the pattern, the common traps, and when offset pagination is the wrong tool.
Summary
- Collection requests return a limited page;
per_pageis capped at 100 for the standard API. - Use
X-WP-TotalPagesor followLinkheaders to know when to stop. - Keep the requested fields small with
_fields, and handle errors and rate limits in your client. - Page-number pagination is suitable for a snapshot-like read; frequent writes during a long export can shift results.
Start with one predictable request
For published posts, a basic request might be:
https://example.com/wp-json/wp/v2/posts?per_page=50&page=1
The API defaults to a smaller page size and supports per_page up to 100. Asking for more than 100 does not turn the endpoint into an unlimited export. Larger requests also increase response size and server work, so choose a page size your client and site can handle.
To reduce the payload, request only what the consumer needs:
https://example.com/wp-json/wp/v2/posts?per_page=50&page=1&_fields=id,date,slug,link,title
The WordPress global-parameters guide documents _fields as a way to return a subset of response fields. If you need related resources, _embed may avoid separate requests, but it also makes each response larger. Measure the trade-off rather than adding it automatically.
Continue until the response says you are done
Paginated responses include X-WP-Total and X-WP-TotalPages headers. The latter is the clearest stop condition. A simple client loop is:
const allPosts = [];
let page = 1;
let totalPages = 1;
do {
const response = await fetch(
`https://example.com/wp-json/wp/v2/posts?per_page=50&page=${page}&_fields=id,date,slug,link,title`
);
if (!response.ok) {
throw new Error(`WordPress returned ${response.status} on page ${page}`);
}
const rows = await response.json();
allPosts.push(...rows);
totalPages = Number(response.headers.get("X-WP-TotalPages") ?? 1);
page += 1;
} while (page <= totalPages);
This example is for a browser or Node.js runtime with fetch. A production importer should also handle network retries, timeouts, JSON errors, authentication if needed, and an unexpected missing header. If your HTTP library hides response headers, configure it to expose them.
Another option is to inspect the response’s Link header and follow the rel="next" URL until it is absent. Following server-provided links can be convenient when endpoint URLs or parameters vary. Whichever method you choose, stop based on the response rather than guessing the number of pages.
Choose filters and ordering deliberately
The endpoint supports resource-specific filters and ordering. For example, order by date or ID when you need a stable sequence supported by that collection. Confirm a parameter against the REST API reference for the resource; not every collection accepts every orderby value.
For a simple one-time export, sort by ID ascending if supported by your endpoint and record the last processed ID. For a recurring sync, store a checkpoint and request changes since that checkpoint where the endpoint supports it. Do not assume pagination is a database snapshot: if posts are created or edited while you are fetching pages, records can move between pages. A repeatable export may need a quiet window, a fixed date boundary, or an application-specific sync strategy.
Handle real-world responses
- Page out of range: the site may contain fewer pages than your count implied. Treat an empty response as a possible end condition, but log it so a changed site is visible.
- Permission errors: public endpoints generally expose published content; private or draft content requires appropriate authentication and permissions. Do not treat a 401 or 403 as an empty collection.
- Large payloads: lower
per_page, use_fields, and avoid embedding unused resources. - Rate limiting or server errors: add bounded retries with backoff for transient failures. Avoid launching dozens of parallel page requests against a small site.
- Custom post types: confirm the post type is exposed in the REST API and check its own endpoint and supported parameters.
A quick validation before relying on the result
After fetching all pages, compare the number of collected records with X-WP-Total when the content set has not changed. Check for duplicate IDs and inspect the first and last records. If counts differ, repeat during a quieter period and log each page number, status code, record count, and total-pages header.
For WordPress sites with many posts, an API may be the right interface but not always the best bulk-export mechanism. If you control the site and need a complete backup, use a supported backup or database/export workflow as well. The REST API is useful for integrations and selective reads, but it does not replace a backup plan.
Related guides: learn how WP-CLI search-replace handles serialized data before changing site URLs, and review what happens to plugin data when you deactivate or delete a plugin before cleaning up a site.
My Take
The bug I see in a lot of small integrations is not a broken endpoint; it is a client that stops after page one. Make the stop condition explicit, keep the payload lean, and log enough to tell a partial import from a complete one.
Conclusion
Set a realistic per_page, follow the pagination metadata, and verify both record count and unique IDs. For live sites, design around records changing while the scan runs. That small amount of care prevents a plausible-looking but incomplete result set.