If you’ve ever wanted to pull WordPress content into an app, static site, or dashboard without dealing with the REST API, WPGraphQL is a powerful plugin that exposes your WordPress data as a GraphQL endpoint. In this guide, we’ll focus on how to query posts directly — including getting a single post or a list of the latest posts — using curl
from the command line.
1. Install WPGraphQL
In your WordPress dashboard:
- Go to Plugins → Add New
- Search for “WPGraphQL”
- Install and activate the plugin
This will create a new endpoint:
https://yourdomain.com/graphql
2. Get the Latest Posts with cURL
Run the following command, replacing the domain with your own:
curl -X POST https://yourdomain.com/graphql \
-H "Content-Type: application/json" \
-d '{
"query": "{ posts(first: 5) { nodes { id title excerpt date uri } } }"
}'
What this does:
- Sends a POST request to the
/graphql
endpoint - Requests the first 5 posts, returning
id
,title
,excerpt
,date
, anduri
3. Get a Single Post by Slug with cURL
If you know the slug of your post (for example, hello-world
), you can fetch it directly:
curl -X POST https://yourdomain.com/graphql \
-H "Content-Type: application/json" \
-d '{
"query": "query GetPostBySlug($slug: ID!) { post(id: $slug, idType: SLUG) { id title content date uri } }",
"variables": { "slug": "hello-world" }
}'
Key points:
post
query fetches a single post instead of an arrayidType: SLUG
tells GraphQL to interpret the ID as a post slug- The
variables
object passes the slug value dynamically
4. Common Field Options
WPGraphQL lets you fetch more than just titles and content. Some useful fields include:
author { node { name } }
→ Get the author’s namefeaturedImage { node { sourceUrl } }
→ Get the post’s featured image URLcategories { nodes { name slug } }
→ Get category data
Example:
{
post(id: "hello-world", idType: SLUG) {
title
author { node { name } }
featuredImage { node { sourceUrl } }
categories { nodes { name slug } }
}
}
5. Why Use WPGraphQL Over REST API?
- Flexible: You choose exactly which fields you want — no over-fetching
- Single request: Get all related data in one query
- Structured: Responses are predictable and match your query shape
Conclusion
With WPGraphQL, you can turn your WordPress site into a modern, headless CMS that’s easy to integrate into apps and frontends. Using curl
is a quick way to test queries before integrating them into your codebase. Once you’ve mastered the basics, you can use advanced GraphQL features like fragments, filtering, and pagination to tailor your data fetching even further.
Leave a Reply