Once you’ve accessed your site and you know how to use authentication (and indeed whether you need to), you’ll need to use one of a range of commands to interact with your site.
The commands you’ll need to use are:
GETretrieves a resource such as a post or some other data.POSTadds a resource to the server, such as a post, attachment or another resource.PUTcan be used to edit or update a resource that’s already on the server.DELETEremoves a resource from the server. Use it with care!
Let’s take a look at each of these in turn.
GET
The GET command is probably the most commonly used: it retrieves data. The example below (which you use once you’ve successfully accessed your site) would fetch a list of all published pages in your site:
GET http://yoursite.com/wp-json/wp/v2/posts/?status=publishedNote that I haven’t included the full path to your site in the line above as you’ve already accessed that using WP-CLI.
Having retrieved that data, you can use it to inform your next step. You might delete one of those posts, edit it, or update it. You could simply output posts to your web app.
Let’s say you wanted to fetch the latest post. You’d use this:
GET http://yoursite.com/wp-json/wp/v2/posts/?per_page=1There are a number of arguments you can use when working with posts. See the WordPress REST API Handbook for more.
POST
Use POST to add new data or resources to your site.
So, for example, if you wanted to create a post, you would start by using a POST command:
POST http://yoursite.com/wp-json/wp/v2/posts/
This would create a new empty draft post.
You can then update the post by using a PUT command to edit it.
With a POST command, you can also add other resources other than posts, including attachments and other post types.
To add a page to your site, you might use something like this:
POST http://yoursite.com/wp-json/wp/v2/posts/pagesThis would create an empty page in just the same way as you would create an empty post.
PUT
The PUT command lets you edit an existing resource, including posts.
Let’s say you have a number of draft posts on your site. You want to check them and update one to make it published.
You could start by fetching a list of all the draft posts:
POST http://yoursite.com/wp-json/wp/v2/posts/?status="draft"The system will give you a list of all current draft posts. You can change the status of one of them using its ID:
PUT http://yoursite.com/wp-json/wp/v2/posts/567This accesses that post and allows you to edit it. You can then change its status using the status argument:
{
"status" = "publish"
}Or you could add content to the post and publish it:
{
"status" = "publish"
"content" = "content here"
}The server will return a 200 - OK status telling you the PUT request has successfully edited the post.
DELETE
The DELETE command does what you would expect: it deletes a resource. By default, if you use it to delete a post it will put it in the trash instead of permanently deleting it.
So if you wanted to move the post you just created to the trash, you would use this:
DELETE http://yoursite.com/wp-json/wp/v2/posts/567However, if you wanted to bypass the trash and delete it permanently, you would use the force argument:
DELETE http://yoursite.com/wp-json/wp/v2/posts/567?force=trueThis permanently deletes the post with no option to undo, so it should be used with caution.

No comments:
Post a Comment