October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk6 min

Headless WordPress with WPGraphQL and Next.js: From First Query to Production

A practical path from activating WPGraphQL and making a first GraphQL query to handling pagination, authentication, previews, caching, and content revalidation in a Next.js frontend.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To connect WordPress to Next.js with WPGraphQL, install and activate the WPGraphQL plugin, inspect your site’s schema in its GraphiQL IDE, and make a paginated query to the site’s GraphQL endpoint. Then have Next.js fetch that data from the server, choose authentication for the request context, and treat previews and cache revalidation as separate production concerns. The fields available depend on your site’s content and enabled extensions, so verify them in GraphiQL rather than assuming another site’s schema applies.

How do I connect WordPress to Next.js with WPGraphQL?

  1. In WordPress, open Plugins > Add New Plugin, search for WPGraphQL, then install and activate it. WPGraphQL describes itself as a plugin that lets developers interact with WordPress data using GraphQL.
  2. Open the GraphiQL IDE provided by the plugin and use its documentation explorer to inspect the schema. Check the content types, fields, and relationships you intend to query; registered content and enabled extensions can change what the schema exposes.
  3. Run a small query in GraphiQL against the actual site. Confirm the response shape and values before writing the frontend query.
  4. In Next.js, make the GraphQL request from server-side code where practical. Store the WordPress endpoint and any credentials in server-only environment variables, not in browser code. The endpoint is commonly the site’s /graphql path when its rewrite configuration is working.

WPGraphQL’s official Quick Start and related guides were checked on October 7, 2026. Host-specific behavior and plugin compatibility can change, so confirm the setup against the versions and hosting environment you deploy.

As an Amazon Associate I earn from qualifying purchases.

How do I make my first WPGraphQL query?

After confirming the fields in GraphiQL, a basic query for a list of posts might look like this:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
query LatestPosts {
  posts(first: 10) {
    nodes {
      id
      title
      uri
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}

This asks for up to ten posts, their identifiers, titles, and paths, plus the cursor information needed to request another page. Use only fields that the site’s schema actually exposes; a custom content model or plugin may require different fields.

Send the query as a GraphQL POST request from your Next.js server code. A minimal fetch pattern is:

const response = await fetch(process.env.WORDPRESS_GRAPHQL_URL, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ query }),
});

if (!response.ok) {
  throw new Error(`WordPress GraphQL request failed: ${response.status}`);
}

const result = await response.json();
if (result.errors?.length) {
  throw new Error("WordPress GraphQL returned an error");
}

return result.data;

Adapt error handling to your app so that GraphQL errors are logged safely and do not expose credentials or private content. If the site uses authenticated queries, add authentication in the server-side request context rather than exposing secrets to the browser.

Paginate collections rather than requesting everything

For a large collection, WPGraphQL’s FAQ recommends cursor pagination with first and after. Request the first page, read endCursor, and use it as the next request’s after value while hasNextPage is true:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
query PostsPage($after: String) {
  posts(first: 10, after: $after) {
    nodes {
      id
      title
      uri
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}

Pass null for $after on the first request and the returned cursor on subsequent requests. This keeps each response bounded and gives the frontend a defined way to load more content; do not treat a single unbounded collection query as a production listing strategy.

Which authentication method fits the request?

Authentication establishes who is making a request; WordPress authorization still decides what that user may access or change. Logging in does not by itself grant access to drafts or permission to run mutations. WPGraphQL’s authentication guidance describes different approaches for different request contexts:

Request context Approach Important constraint
Remote or server-to-server frontend request Application passwords Keep credentials on the server and use an account with only the capabilities the integration needs.
Remote request using an extension-based token flow JWT through an authentication extension Install and configure the relevant extension; token authentication does not bypass WordPress capability checks.
Browser request made in a logged-in WordPress context Cookie-based authentication Include a nonce for CSRF protection. Do not treat a cookie alone as sufficient for a state-changing request.

Never put passwords, tokens, or other credentials in a URL or query string. For a decoupled Next.js site, keep privileged requests on the server unless the intended user interaction specifically requires an authenticated browser request.

How should previews work?

A preview is a privileged request, not a public content fetch with a different URL. WPGraphQL’s current preview guide recommends the X-GraphQL-Preview request header instead of the deprecated asPreview argument. Follow the installed guide for the header’s expected value and send it only from the preview request path.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The request must also be authenticated as a WordPress user who can edit the target post. The guide describes preview content as an overlay from the newest autosave while retaining the published post’s identity. A preview marker or nonce alone does not grant permission to read a draft.

WordPress users and external stakeholders are different audiences

The documented WPGraphQL preview mechanism does not supply account-less preview links. Editors with the required WordPress capability can use the authenticated preview flow. If clients or other stakeholders without WordPress accounts need to review unpublished pages, build an application-level gated preview flow: authorize access on the Next.js server, protect the preview route and its data request, and ensure the gate cannot be bypassed by guessing a URL.

How do I keep preview and public caching separate?

WPGraphQL preview responses use Cache-Control: no-store, private and Vary: X-GraphQL-Preview. Your CDN, reverse proxy, and any application cache must honor these response headers or explicitly bypass caching for preview requests. If an intermediary ignores them, it can serve private preview data to a public request, or show published data where an editor expects a preview.

Test the full path through the production cache layer, not only the WordPress origin: make an authorized preview request, then a public request for the same content, and verify that each receives the correct response. Do not cache authenticated preview responses in a shared cache.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What needs to be configured at the WordPress origin?

  • Permalinks and rewrite rules: WPGraphQL relies on WordPress rewrite rules for the /graphql endpoint. The compatibility guidance recommends a permalink mode other than Plain; confirm that the endpoint resolves after changing permalink settings.
  • HTTPS: Use HTTPS for production requests between the frontend and WordPress, particularly when authentication is involved.
  • Host support: Network-cache features may depend on the hosting environment. Check whether the host supports the specific cache behavior you plan to use rather than assuming a feature works at every provider.
  • Version compatibility: Compatibility ranges and host behavior are version- and environment-dependent. Verify them against the current WPGraphQL and hosting documentation during deployment.

The compatibility guide lists Next.js through FaustJS among headless frontend options, but that does not make FaustJS a requirement: a Next.js application can make its own requests to WPGraphQL.

How do I make WordPress changes appear in Next.js?

Fetching content successfully is only part of production correctness. A page may remain stale after an editor publishes or updates content unless the frontend refreshes its cached data. Two common freshness strategies are:

Strategy How it works Trade-off
Periodic refresh The frontend refreshes or revalidates content on a schedule. Simple to reason about, but a change may not appear until the next refresh interval.
Event-triggered revalidation A WordPress cache-invalidation event calls a protected endpoint in the frontend to revalidate affected content. Can update content closer to the change, but requires a secure webhook path and a reliable mapping from changed content to frontend routes.

WPGraphQL Smart Cache documents an on-demand pattern that handles its graphql_purge action and calls a frontend revalidation API, using Next.js as the example. In your implementation, make the event-to-route mapping explicit: determine which frontend paths depend on the changed WordPress content, and revalidate those paths rather than blindly refreshing unrelated pages.

Protect the revalidation endpoint with a secret or equivalent server-side authentication. Keep that secret out of client-side code, reject unauthenticated calls, and avoid accepting arbitrary route data that could trigger unintended work. Confirm the exact Next.js revalidation API and route-handler syntax for the version your application runs; those details are framework-version specific.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Production launch checks

  • GraphiQL confirms every field used by the frontend query.
  • Collection queries use bounded pages and follow cursors when more results exist.
  • Credentials stay server-side, and each integration account has only the capabilities it needs.
  • Cookie-authenticated browser requests include a nonce for CSRF protection.
  • Preview requests are authenticated, authorized by edit capability, and kept out of shared caches.
  • The WordPress endpoint resolves through the deployed rewrite and permalink configuration over HTTPS.
  • Content changes trigger the selected refresh strategy, and event-triggered revalidation is authenticated and maps changed content to the right frontend paths.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.