Skip to main content
next.js

How to Build a Blog with Next.js and Headless WordPress

Published: | Tags: wordpress, headless cms, next.js

Combining Next.js with Headless WordPress is one of the most powerful ways to create a modern blog that delivers speed, flexibility, and scalability. Instead of using WordPress as a monolithic CMS where backend and frontend are tied together, the headless approach decouples content management from presentation. This allows developers to take advantage of WordPress’s robust content editor while leveraging Next.js for performance-driven, SEO-friendly frontends.

Why Choose Headless WordPress with Next.js?

  • Performance: Static site generation and server-side rendering in Next.js ensure faster loading times.
  • Scalability: Easily handle traffic spikes with incremental static regeneration.
  • Flexibility: Use WordPress only as a content hub while designing the frontend with React.
  • SEO Benefits: Next.js enhances site indexing and overall discoverability.

To start, you need to set up a WordPress instance that will act as the headless CMS. You can do this either on a traditional hosting provider or using a managed WordPress service. Once installed, you’ll connect it to the WordPress REST API or GraphQL endpoint (via plugins like WPGraphQL). This connection will allow your Next.js app to fetch posts, pages, and metadata directly from WordPress.

Before moving on, ensure your WordPress site is accessible via the REST API (default endpoint: /wp-json) or configured with WPGraphQL for more structured queries.

Next, set up a Next.js project. You can bootstrap it quickly using the command:

npx create-next-app my-blog

This initializes your frontend framework. From here, you’ll integrate WordPress as the data source, creating a foundation for a fully functional headless blog. In the next part, we’ll cover fetching content from WordPress and rendering it dynamically inside your Next.js pages.

For related reading, check out our article on best headless CMS platforms to explore additional CMS options beyond WordPress.

Fetching WordPress Content into Next.js

Once your Next.js project is initialized, the next step is connecting it to WordPress so your blog can pull posts, categories, and metadata. The most common way is by using the WordPress REST API or WPGraphQL. Both provide structured endpoints to retrieve content from WordPress without relying on its native frontend.

Setting Up API Access

By default, every WordPress installation has a REST API accessible at:

https://your-wordpress-site.com/wp-json/wp/v2/posts

This endpoint returns your blog posts in JSON format. If you’re using WPGraphQL, you’ll instead query with GraphQL syntax at the endpoint /graphql.

Fetching Data with Next.js

Inside your Next.js project, you can use the getStaticProps or getServerSideProps functions to fetch data at build time or request time. Here’s an example using the REST API:

export async function getStaticProps() {
  const res = await fetch('https://your-wordpress-site.com/wp-json/wp/v2/posts');
  const posts = await res.json();
  return {
    props: { posts },
  };
}

Then you can pass these posts into your blog component and render them:

export default function Blog({ posts }) {
  return (
    <div>
      {posts.map(post => (
        <article key={post.id}>
          <h2 dangerouslySetInnerHTML={{ __html: post.title.rendered }} />
          <div dangerouslySetInnerHTML={{ __html: post.excerpt.rendered }} />
        </article>
      ))}
    </div>
  );
}

Be careful with dangerouslySetInnerHTML in React. Make sure to sanitize HTML from WordPress if you don’t fully trust all input.

Using GraphQL for More Control

If you want more structured queries, install the WPGraphQL plugin on your WordPress site. This allows you to query only the fields you need:

{
  posts {
    nodes {
      id
      title
      excerpt
      date
    }
  }
}

GraphQL works well with Next.js because it prevents overfetching and gives you fine-grained control over data. Libraries like graphql-request or Apollo Client can be used to manage queries inside your app.

In the next part, we’ll focus on designing the blog layout, adding SEO features, and deploying the site so your blog is both performant and ready for production.

If you want to compare hosting strategies before deploying, you can read our guide on Cloud vs Traditional Hosting.

Design, SEO, and Deployment

With your WordPress content successfully integrated into Next.js, the final steps are about designing the blog layout, adding SEO optimizations, and preparing for deployment. These aspects ensure your blog is not only functional but also engaging, visible to search engines, and performant at scale.

Designing the Layout

  • Global Layout: Create a main layout component with navigation, footer, and consistent styling.
  • Article Pages: Use dynamic routes in Next.js (e.g., pages/posts/[id].js) to generate pages for each blog post.
  • Styling: Consider Tailwind CSS or styled-components for responsive, modern UI.
  • Media: Leverage WordPress as the media library and render images with Next.js <Image> for optimization.

Adding SEO Enhancements

Next.js provides built-in components for SEO. Use the next/head component to insert meta tags, titles, and structured data:

import Head from 'next/head';

<Head>
  <title>My Next.js Blog</title>
  <meta name="description" content="A headless WordPress blog built with Next.js." />
</Head>

Additionally, consider:

  • Generating sitemaps automatically with libraries like next-sitemap.
  • Using Open Graph tags for better social media previews.
  • Optimizing performance scores with Lighthouse.

Great SEO ensures your content ranks well and attracts organic traffic, turning your blog into a reliable growth engine.

Deploying Your Blog

The easiest way to deploy a Next.js + WordPress blog is through Vercel, the platform behind Next.js. It offers zero-config deployment, continuous integration, and edge caching. Alternatively, you can use Netlify or self-host with Docker.

Steps to deploy on Vercel:

  1. Push your Next.js project to GitHub or GitLab.
  2. Connect the repository to Vercel.
  3. Set environment variables for your WordPress API endpoints.
  4. Trigger the build and enjoy automatic deployment.

Meanwhile, your WordPress instance can stay on a managed host like Kinsta, WP Engine, or a self-hosted VPS. Just make sure the API endpoint is publicly accessible.

Final Thoughts

By decoupling WordPress with Next.js, you gain the best of both worlds: a user-friendly CMS for editors and a high-performance React frontend for users. This architecture ensures flexibility, scalability, and modern web standards. Whether you’re building a personal blog or a large-scale publishing platform, this stack delivers speed and control.

For deeper insights into hosting solutions, explore our detailed guide on shared hosting pros and cons before you choose where to run your backend.