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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To connect WordPress to Next.js with WPGraphQL, install and activate the WPGraphQL plugin, inspect the live schema in its GraphiQL IDE, and send GraphQL requests from your Next.js server to your WordPress site’s /graphql endpoint. A production-ready setup also needs cursor pagination, authentication matched to the request context, protected previews, cache isolation, and a plan for revalidating pages when content changes.

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

WPGraphQL is a WordPress plugin that exposes WordPress data through a GraphQL API. Next.js can request that data from the WordPress origin and render it in the frontend. Start by confirming that the API works on the actual WordPress installation; do not assume every site has the same schema or hosting configuration.

1. Install WPGraphQL and check the endpoint

  1. In the WordPress dashboard, go to Plugins > Add New Plugin, find WPGraphQL, install it, and activate it.
  2. Open the GraphiQL IDE made available by WPGraphQL. Use it to check that the API responds and to explore the documentation explorer for the site’s available types and fields.
  3. Confirm that the WordPress site’s permalink setting is not Plain. WPGraphQL relies on WordPress rewrite rules for the /graphql route. For production, use HTTPS.

If the endpoint does not resolve, verify the permalink configuration and the host’s rewrite behavior before debugging the Next.js application. Hosting support for network-cache features can vary, so check the capabilities of the particular environment you deploy to.

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

2. Inspect the schema before building a page

GraphQL fields are determined by the site’s registered content and enabled extensions. A field found in an example or on another WordPress site may not exist on yours. In GraphiQL, inspect the relevant types, fields, and arguments, then run the query against the live schema before using it in frontend code.

3. Make a first query

This example requests a page of posts and the cursor information needed to request another page. Verify the field names and types in GraphiQL for your installation.

query FirstPostsPage {
  posts(first: 10) {
    nodes {
      id
      title
      uri
    }
    pageInfo {
      endCursor
      hasNextPage
    }
  }
}

For a query that takes variables, GraphQL requests are commonly sent as a JSON object containing a query string and a variables object. A small server-side fetch helper in a Next.js project can look like this:

const endpoint = process.env.WORDPRESS_GRAPHQL_URL;

export async function fetchGraphQL(query, variables = {}) {
  if (!endpoint) {
    throw new Error("WORDPRESS_GRAPHQL_URL is not configured");
  }

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

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

  const result = await response.json();
  if (result.errors?.length) {
    throw new Error(result.errors.map((error) => error.message).join("; "));
  }

  return result.data;
}

Set WORDPRESS_GRAPHQL_URL in the Next.js server environment to the site’s HTTPS GraphQL endpoint. Keep server-only secrets out of browser bundles. The helper deliberately surfaces HTTP and GraphQL errors rather than silently treating an unsuccessful request as an empty content result.

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

How do I make the query safe and complete for real content?

Request only what the page uses

GraphQL lets a page ask for selected fields, so include only the data that rendering requires. This keeps the contract between the page and the schema explicit and makes missing or renamed fields easier to identify.

Paginate collections with cursors

Do not treat a single large collection query as a production content strategy. WPGraphQL’s FAQ recommends first and after for large datasets. Use the returned endCursor as the next request’s after value, and continue only while hasNextPage is true. This makes the list’s completeness explicit rather than relying on an arbitrary oversized result.

For example, the next request can use variables instead of embedding cursor values into the query:

query PostsPage($count: Int!, $cursor: String) {
  posts(first: $count, after: $cursor) {
    nodes {
      id
      title
      uri
    }
    pageInfo {
      endCursor
      hasNextPage
    }
  }
}

Supply the page size and cursor through the request’s variables object. Confirm supported arguments and field types in the live schema.

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

Which authentication method should the frontend use?

Choose authentication based on where the request runs and whose identity it needs. Authentication proves a request’s identity; it does not by itself authorize access to every post or mutation. WordPress capability checks still determine what that user may do.

Request context Approach described by WPGraphQL Important condition
Remote or server-to-server request Application passwords Keep credentials on the server and out of query parameters.
JWT-based integration JWT through an extension Use an authentication integration appropriate to the deployment; authentication does not bypass WordPress capabilities.
Logged-in browser request using WordPress cookies Cookie-based authentication Include a nonce for CSRF protection.

For a public content request that needs no privileged access, avoid adding credentials unnecessarily. For private content, drafts, or mutations, use an appropriate authenticated request and check that the WordPress user has the capability required for the operation. Never put credentials in a URL or GraphQL query string.

How should previews work?

Preview is a privileged request context, not simply a public query with a different flag. WPGraphQL’s current preview guidance uses the X-GraphQL-Preview request header; the older asPreview argument is deprecated.

Preview for an authorized WordPress user

The preview request must be authenticated, and its user must be able to edit the target post. WPGraphQL resolves previewable content using the newest autosave while retaining the published post’s identity. A valid login or preview header alone does not grant access: the user’s edit capability is the authorization boundary.

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

Preview for stakeholders without WordPress accounts

The WPGraphQL preview mechanism does not provide account-less preview links. If editors need to share a preview with stakeholders who do not have WordPress accounts, the headless application must provide its own gated server-side access flow. Do not make privileged preview credentials available to the browser or expose a private preview endpoint as a public content API.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How do I handle caching and content changes in production?

Keep preview responses out of shared caches

WPGraphQL preview responses use Cache-Control: no-store, private and Vary: X-GraphQL-Preview. A CDN or reverse proxy in front of WordPress must honor these headers or bypass caching for preview requests. Otherwise, a shared cache can serve private preview data or mix preview and published responses.

Choose a freshness strategy for published pages

For published content, decide how the frontend will become fresh after an edit. Periodic refresh is one option; event-triggered revalidation is another. WPGraphQL Smart Cache documents a pattern in which its graphql_purge action is handled to call a frontend revalidation API, with Next.js shown as the example.

In that pattern, define the content-to-route mapping explicitly: a changed post may affect its own page, an archive, a category listing, or another page that includes it. Secure the revalidation endpoint with a secret, keep that secret server-side, and validate incoming requests before triggering revalidation. The exact route and API implementation depends on the Next.js version and application setup.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Verify the origin and cache behavior after deployment

  • Confirm that the production WordPress origin serves the GraphQL endpoint over HTTPS and that rewrite rules resolve it.
  • Test a public query and an authenticated preview separately; check that preview responses are not stored by intermediate caches.
  • Trigger a content change and verify that the intended frontend routes become fresh.
  • Check host support for any network-cache features you plan to use; availability and behavior can differ by host and deployment.

Production checklist

  • WPGraphQL is installed and active, and GraphiQL confirms the actual schema.
  • The WordPress permalink mode is not Plain, and the production origin uses HTTPS.
  • Queries request the fields the page needs and paginate large collections with first and after.
  • Authentication matches the request context; credentials remain server-side where possible, and cookie-authenticated browser requests include a nonce.
  • Preview requests use X-GraphQL-Preview, require authenticated edit capability, and bypass shared caching.
  • Content-change revalidation has an explicit route mapping and a protected endpoint.

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.