I’m using the wordpress api to create new posts. How can I create a revision of an existing post instead of creating a new one?
Creating a revision of an existing post using the WordPress REST API involves updating the post rather than creating a new one. The WordPress REST API does not explicitly have an endpoint for creating revisions; however, revisions are automatically generated when you update a post. Here’s how you can achieve this:
Step-by-Step Guide
-
Authenticate with the API:
Ensure you are authenticated. WordPress REST API requires authentication to update posts. You can use OAuth, Application Passwords, or JWT tokens for this purpose. -
Get the Post ID:
You need the ID of the post you wish to create a revision for. You can retrieve this from your posts list if you don’t have it already. -
Send a POST Request to Update the Post:
To update (and thereby create a revision), you’ll send a POST request to the endpointhttps://yoursite.com/wp-json/wp/v2/posts/<post_id>
, where<post_id>
is the ID of the post you’re updating.Here’s an example using
curl
:curl -X POST https://yoursite.com/wp-json/wp/v2/posts/<post_id> \ -H "Content-Type: application/json" \ -u username:password \ -d '{ "title": "Updated Post Title", "content": "Updated post content.", "status": "publish" }'
Replace
username:password
with your authentication credentials, and modify the JSON payload as needed. -
Check the Update Response:
If the update is successful, the API will return a response containing the updated post data, which confirms that a revision was created automatically.
Additional Considerations
-
Content Drafts: If you want to make changes without publishing immediately, consider using
"status": "draft"
in your API request. -
Versioning: Keep track of changes and ensure you update the correct version of the post, especially if multiple people are working on the same site.
-
Handling Conflicts: Be mindful of potential edit conflicts, especially on a site with multiple contributors. Implement logic to handle these scenarios.
By following these steps, you can update an existing post via the WordPress REST API, which will automatically handle the creation of a new revision.
show an example of just creating a new post. Does it return a post id so I can use it later when I update?
Certainly! When you create a new post using the WordPress REST API, it returns a response containing the details of the newly created post, including the post ID. This ID can then be used for any future updates or modifications to the post.
Here’s how you can create a new post using WordPress REST API with an example using curl
:
Example: Creating a New Post
-
Endpoint:
The endpoint for creating a new post ishttps://yoursite.com/wp-json/wp/v2/posts
. -
POST Request:
Use a POST request to this endpoint with your post data. You’ll need to include your authentication credentials as well. -
Example Request:
curl -X POST https://yoursite.com/wp-json/wp/v2/posts \ -H "Content-Type: application/json" \ -u username:password \ -d '{ "title": "My New Post", "content": "This is the content of my new post.", "status": "publish" }'
Replace
username:password
with your method of authentication (such as your WordPress username and an application password if you’re using Basic Auth). -
Response:
If successful, the API will return a JSON object with details of the created post. Here’s an example response:{ "id": 123, "date": "2023-10-15T12:34:56", "slug": "my-new-post", "status": "publish", "title": { "rendered": "My New Post" }, "content": { "rendered": "<p>This is the content of my new post.</p>" }, ... }
The
"id": 123
indicates the ID of the newly created post. You can store this ID for use in future API calls to update or delete the post.
Additional Options
- Draft Posts: If you don’t want to publish the post immediately, set
"status": "draft"
in the JSON payload. - Custom Fields and Taxonomies: You can include additional fields, such as categories, tags, or custom fields, in the JSON payload if needed.
Make sure to replace "https://yoursite.com"
with the actual URL of your WordPress site and adjust the authentication method to match the setup on your site.
show an example of creating a new post with a category and a tag.
To create a new post with a specific category and tag using the WordPress REST API, you’ll need to specify their IDs in your request.
Here’s how you can include categories and tags when creating a post:
Step-by-Step Guide
-
Determine the Category and Tag IDs:
- Before creating a post, you’ll need to know the IDs of the categories and tags you want to associate with the post. You can retrieve these IDs via the API:
- Categories:
GET https://yoursite.com/wp-json/wp/v2/categories
- Tags:
GET https://yoursite.com/wp-json/wp/v2/tags
- Categories:
- Before creating a post, you’ll need to know the IDs of the categories and tags you want to associate with the post. You can retrieve these IDs via the API:
-
Create the New Post with Categories and Tags:
- Use the POST endpoint for posts and include the
categories
andtags
fields in your JSON payload.
- Use the POST endpoint for posts and include the
Example Request
Here’s an example using curl
:
curl -X POST https://yoursite.com/wp-json/wp/v2/posts \
-H "Content-Type: application/json" \
-u username:password \
-d '{
"title": "My New Post with Categories and Tags",
"content": "This is the content of my new post.",
"status": "publish",
"categories": [1, 2], // Replace with actual category IDs
"tags": [3, 4] // Replace with actual tag IDs
}'
Explanation:
title
: The title of your new post.content
: The content of your post.status
: Set to"publish"
to immediately publish the post. Use"draft"
to save it as a draft instead.categories
: An array of category IDs that you want to associate with the post.tags
: An array of tag IDs that you want to associate with the post.
Retrieving Category and Tag IDs
To determine the IDs for categories and tags:
-
Categories:
curl -X GET https://yoursite.com/wp-json/wp/v2/categories
-
Tags:
curl -X GET https://yoursite.com/wp-json/wp/v2/tags
Each request will return JSON objects containing category or tag details, including their IDs, which you can use in your post creation request.
Make sure to replace username:password
with your actual authentication credentials and adjust the content, categories, and tag IDs to suit your setup.
Is it possible add a tag by passing the string value of the tag?
Yes, you can add a tag by passing the string value of the tag in the WordPress REST API request. When you specify a tag by its string name, WordPress will first check if the tag already exists. If it does, it will use the existing tag; if it doesn’t, WordPress will create it and associate it with the post.
Here’s how you can do it:
Creating a New Post with a Tag Specified by String Name
-
POST Endpoint:
Use the POST request to thehttps://yoursite.com/wp-json/wp/v2/posts
endpoint. -
JSON Payload:
Specify the tags by their string names using thetags
attribute. The names should be passed as strings within an array fortags
.
Here’s an example using curl
:
curl -X POST https://yoursite.com/wp-json/wp/v2/posts \
-H "Content-Type: application/json" \
-u username:password \
-d '{
"title": "My New Post with Tagged Content",
"content": "This is the content of my post with string-specified tags.",
"status": "publish",
"tags": ["my-awesome-tag", "another-tag"]
}'
Key Points
- Title: The title of your post.
- Content: The main content body of your post.
- Status: You can set it to "publish", "draft", etc., depending on your requirement.
- Tags: An array of string values representing the names of tags you want to associate with the post.
Additional Considerations
-
Authorization: Make sure that your authentication method (such as Basic Auth, OAuth, etc.) is correctly set up and has the required permissions to create posts and tags.
-
Existing Tags: If
my-awesome-tag
oranother-tag
already exist, WordPress will reuse them. Otherwise, they will be created. -
Categories by Name: Note that categories cannot be added by name in the same way; you must use IDs for categories, hence you may need to create them first or fetch their IDs.
By following these steps, you can create a new post and conveniently add tags using their string names in the WordPress REST API.
Leave a Reply