-
Notifications
You must be signed in to change notification settings - Fork 295
Description
Summary
When using the posts/[slug] page, the app fails with a 404 / TimeoutError if the WordPress REST endpoint /wp-json/wp/v2/users/:id is disabled (which is common in production WordPress setups).
This happens because the template fetches author, category, and featured media via separate REST requests, instead of using WordPress’ _embed=true feature.
Environment
- Template:
9d8dev/next-wp - Framework: Next.js (App Router)
- WordPress: Headless CMS
- REST API security:
/wp/v2/usersendpoint disabled (default on many hosts/plugins)
Error Output
Error [WordPressAPIError]: WordPress API request failed: Not Found
endpoint: https://example.com/wp-json/wp/v2/users/4
status: 404
and sometimes followed by:
Error [TimeoutError]: The operation was aborted due to timeout
Steps to Reproduce
- Use a WordPress site where
/wp-json/wp/v2/users/:idis disabled or protected - Navigate to
/posts→ works fine - Click on a single post →
/posts/[slug] - Page fails with 404 or timeout error
Root Cause
In app/posts/[slug]/page.tsx, the template performs multiple REST requests:
const author = await getAuthorById(post.author);
const featuredMedia = await getFeaturedMediaById(post.featured_media);
const category = await getCategoryById(post.categories[0]);This assumes all REST endpoints are publicly accessible, which is not true for many WordPress installations, especially /users.
Expected Behavior
- Post detail page should render correctly even if
/wp/v2/usersis disabled - Author, category, and featured media should be resolved from the post request itself
Suggested Fix
Use WordPress REST API embedding:
/wp-json/wp/v2/posts?slug=example&_embed=trueThen resolve related entities from _embedded instead of separate fetches:
- Author →
_embedded.author[0] - Featured image →
_embedded["wp:featuredmedia"][0] - Categories →
_embedded["wp:term"]
This approach:
- Eliminates extra REST requests
- Prevents 404 / timeout errors
- Matches WordPress headless best practices (used by Frontity, Faust.js, etc.)
Proposed Code Changes (High Level)
-
Update
getPostBySlugto include_embed=true -
Refactor
posts/[slug]/page.tsxto stop calling:getAuthorByIdgetFeaturedMediaByIdgetCategoryById
-
Read related data from
post._embedded
Additional Context
This issue does not appear on the posts list page because pagination endpoints work without hitting /users. The issue only occurs on post detail pages.