DEV Community
Follow
WordPress REST API Basics: What Can You Do With /wp-json/?
WordPress has a built-in REST API since version 4.7, eliminating the need for plugins to access its content remotely. By appending /wp-json/ to a site's URL, one can discover available data endpoints. The /wp/v2/posts endpoint is commonly used to retrieve published posts as JSON data. This endpoint does not require authentication for read-only access to publicly visible content. Query parameters like per_page allow for control over the number of results returned. The _embed parameter is crucial for fetching related data, such as featured images and category names, in a single request. This avoids multiple separate API calls for each piece of related information. A practical example demonstrates how a landing page can pull the latest posts using this API without understanding the blog's internal structure. To improve performance and reduce load, the API response is cached for one hour. This caching mechanism also includes a graceful fallback to stale cache data if the API becomes unavailable. Authentication is strictly enforced for endpoints that modify data, such as creating or deleting posts, using nonces or Application Passwords.