0
0
WordpressHow-ToBeginner · 4 min read

How to Create a Post Using REST API in WordPress

To create a post in WordPress using the REST API, send a POST request to /wp-json/wp/v2/posts with proper authentication and JSON data including the post title and content. Use Basic Auth or OAuth for authentication and include fields like title, content, and status in the request body.
📐

Syntax

Use a POST request to the endpoint /wp-json/wp/v2/posts with a JSON body containing post details. Authentication is required to create posts.

  • URL: Your site URL + /wp-json/wp/v2/posts
  • Method: POST
  • Headers: Include Authorization header for authentication and Content-Type: application/json
  • Body: JSON with keys like title, content, and status
http
POST /wp-json/wp/v2/posts HTTP/1.1
Host: example.com
Authorization: Basic base64_encode('username:password')
Content-Type: application/json

{
  "title": "Your Post Title",
  "content": "Your post content here.",
  "status": "publish"
}
💻

Example

This example shows how to create a new published post using curl with Basic Authentication. Replace example.com, username, and password with your WordPress site and user credentials.

bash
curl -X POST https://example.com/wp-json/wp/v2/posts \
-H "Authorization: Basic $(echo -n 'username:password' | base64)" \
-H "Content-Type: application/json" \
-d '{
  "title": "Hello from REST API",
  "content": "This post was created using the WordPress REST API.",
  "status": "publish"
}'
Output
{ "id": 123, "date": "2024-06-01T12:00:00", "title": {"rendered": "Hello from REST API"}, "content": {"rendered": "<p>This post was created using the WordPress REST API.</p>"}, "status": "publish" }
⚠️

Common Pitfalls

  • Missing Authentication: The REST API requires authentication to create posts; without it, you get a 401 Unauthorized error.
  • Incorrect Endpoint: Using the wrong URL or HTTP method will fail; ensure you use /wp-json/wp/v2/posts with POST.
  • Invalid JSON: Malformed JSON in the request body causes errors; always validate your JSON.
  • Insufficient Permissions: The user must have the capability to create posts (e.g., Editor or Administrator role).
bash
Wrong way (missing auth):
curl -X POST https://example.com/wp-json/wp/v2/posts \
-H "Content-Type: application/json" \
-d '{"title": "Test", "content": "No auth"}'

Right way (with auth):
curl -X POST https://example.com/wp-json/wp/v2/posts \
-H "Authorization: Basic $(echo -n 'username:password' | base64)" \
-H "Content-Type: application/json" \
-d '{"title": "Test", "content": "With auth"}'
📊

Quick Reference

Remember these key points when creating posts via the WordPress REST API:

  • Use POST /wp-json/wp/v2/posts endpoint.
  • Authenticate using Basic Auth or OAuth.
  • Send JSON body with title, content, and status.
  • Set status to publish to make the post live immediately.
  • Ensure user has permission to create posts.

Key Takeaways

Use POST request to /wp-json/wp/v2/posts with authentication to create a post.
Include JSON body with title, content, and status fields.
Authentication is mandatory; without it, the request will fail.
Ensure the user role has permission to create posts.
Validate JSON and use correct endpoint and HTTP method.