01 logo
Content warning
This story may contain sensitive material or discuss topics that some readers may find distressing. Reader discretion is advised. The views and opinions expressed in this story are those of the author and do not necessarily reflect the official policy or position of Vocal.

How to Use WordPress as a Headless CMS for Next.js

A practical, SEO-focused setup using WPGraphQL or the WordPress REST API—with ISR, previews, and performance tips

By Anouar AissaouiPublished 3 months ago 4 min read
How to Use WordPress as a Headless CMS for Next.js

If you love the WordPress editorial experience but want the speed and flexibility of React, pairing WordPress as a headless CMS with Next.js is a powerful move. In this guide, you’ll learn the fastest, production-ready way to connect WordPress to Next.js using either the built-in WordPress REST API or the popular WPGraphQL plugin, then add Incremental Static Regeneration (ISR), secure previews, structured data, and performance best practices.

What you’ll learn

          • Pros/cons of REST vs. GraphQL for WordPress
          • How to fetch posts/pages with Next.js App Router
        • How to wire ISR, on‑demand revalidation, and previews
      • How to handle images, ACF, and SEO metadata
    • Deploying to Vercel and hardening your setup

    Why use WordPress as a headless CMS for Next.js

    Using WordPress for content and Next.js for the front end gives you:

  • Better performance with pre-rendering via SSG/ISR (see Next.js docs on Incremental Static Regeneration: https://nextjs.org/docs/app/building-your-application/data-fetching/incremental-static-regeneration)
    • A secure split—your WordPress admin isn’t serving the public site
    • A familiar editor, custom fields, and roles
    • Developer-friendly React components, routing, and TypeScript

    Learn more about the platform capabilities here:

    WordPress REST API vs WPGraphQL (which should you choose?)

    Both work great—choose based on complexity and flexibility needs.

    REST API (long‑tail keyword: “WordPress REST API with Next.js

    • Built in, no plugin required
    • Endpoints like /wp-json/wp/v2/posts, /pages, /media
    • Simpler onboarding but more requests and potential over‑fetching

    WPGraphQL (long‑tail keyword: “WPGraphQL with Next.js App Router

Set up WordPress for headless (long‑tail keyword: “headless WordPress configuration

Create your Next.js project (long‑tail keyword: “Next.js headless WordPress setup

Environment variables (example)

Allow WordPress media domains for optimized images:

next/image docs: https://nextjs.org/docs/app/api-reference/components/image

Fetch content in Next.js (long‑tail keyword: “fetch WordPress content in Next.js

Option 1 — WPGraphQL query pattern

  • Install WPGraphQL and verify /graphql is live
  • Build a tiny fetcher and query posts by slug

Example (abbreviated)

  • Fetcher (POST JSON to your GraphQL endpoint)
  • Query posts list (id, slug, title, excerpt, featuredImage)
  • Query single post by SLUG (title, content, featured image, ACF SEO fields)

Authoritative references:

Option 2 — REST API query pattern

  • List posts: /wp-json/wp/v2/posts?per_page=20
  • Get by slug: /wp-json/wp/v2/posts?slug=your-slug
  • Use Next.js revalidate for ISR

Reference:

  • WordPress REST API: https://developer.wordpress.org/rest-api/
  • Incremental Static Regeneration and instant updates

    Static pages are fast, but your editors want instant updates after publishing.

    Set route-level revalidate (ISR)

On‑demand revalidation (long‑tail keyword: “WordPress webhook Next.js revalidate

Previews for drafts (long‑tail keyword: “Next.js preview mode with WordPress

Images, media, and performance

  • Use next/image for responsive, lazy-loaded images
  • Add WordPress upload domains to next.config.js
  • Prefer 1200×630 hero images (OG-ready)
  • Optimize Core Web Vitals by pre-rendering content and limiting client-side fetching

Custom Post Types and ACF (long‑tail keyword: “ACF fields in WPGraphQL”)

  • Create CPTs and ACF field groups for structured content
  • Expose via WPGraphQL for ACF for typed queries
  • Map fields to purpose-built React components
  • Docs: https://github.com/wp-graphql/wp-graphql-acf

SEO and structured data for headless WordPress

Sitemaps and robots

  • Generate a simple sitemap.ts in Next.js that lists your home, blog index, and posts
  • Add robots.txt and point to your sitemap
  • Reference: Google sitemaps overview: https://developers.google.com/search/docs/crawling-indexing/sitemaps/overview

Deploy and harden

Common pitfalls (and how to avoid them)

  • No instant updates after publish → Add on‑demand ISR with a WordPress webhook
  • Previews returning old content → Ensure draftMode bypasses caches
  • Missing images in production → Allowlist your WordPress media domain
  • Over-fetching content → Prefer WPGraphQL with precise field queries for complex models
  • Slug conflicts across CPTs → Namespace routes (e.g., /blog/[slug] vs /case-studies/[slug])

Quick code snippets (Vocal-friendly, abbreviated)

Minimal WPGraphQL fetcher

  • POST JSON to /graphql
  • Throw on errors; return data
  • Use server-only requests (avoid exposing secrets client-side)

REST post by slug

  • GET /wp-json/wp/v2/posts?slug={slug}
  • In Next.js, fetch with { next: { revalidate: 300 } }
  • H3: On‑demand revalidate endpoint (App Router)
  • /api/revalidate?secret=...&path=/blog/my-post
  • Validate secret; call revalidatePath(path)

Pro tip: Host long code samples in a GitHub Gist and embed or link from Vocal for a cleaner reading experience.

tech news

About the Creator

Anouar Aissaoui

I'm a results-driven Full Stack Web Developer with deep expertise in building high-performance, responsive, and SEO-optimized websites. Backed by years of hands-on experience, I specialize in WordPress development and strategic SEO

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2026 Creatd, Inc. All Rights Reserved.