Skip to content

Post detail page fails when WordPress /users REST endpoint is disabled #75

@presidenwashil

Description

@presidenwashil

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/users endpoint 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

  1. Use a WordPress site where /wp-json/wp/v2/users/:id is disabled or protected
  2. Navigate to /posts → works fine
  3. Click on a single post → /posts/[slug]
  4. 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/users is 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=true

Then 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 getPostBySlug to include _embed=true

  • Refactor posts/[slug]/page.tsx to stop calling:

    • getAuthorById
    • getFeaturedMediaById
    • getCategoryById
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions