Check out a free preview of the full JavaScript for WordPress course:
The "Deleting Posts" Lesson is part of the full, JavaScript for WordPress course featured in this preview video. Here's what you'd learn in this lesson:

After showing how to save posts, Zac quickly shows how to delete posts.

Get Unlimited Access Now

Transcript from the "Deleting Posts" Lesson

>> Zac Gordon: That completes the loop for this and from here on out it's pretty easy. I'm gonna jump ahead to the very end and just walk through the extra code that you'll see there. We have a delete function which is running Axios with a method of delete. It's passing in the URL to a specific id, it's giving in our JSON format and token.

[00:00:26] And then after our response we're gonna load a message that says deleted and reload posts. So that's delete, pretty straight forward. And we also have, rather than doing a separate update post and a save post, because technically you could use the same method in Axios. No, no, we could do different ones, but save could be and probably should be maybe its own class.

[00:00:56] And then update could be a separate one. But just like we did before, I'm gonna run a conditional and check to see, hey, is something available on the page? Is a certain thing set? And if it is, I know it's on a single page. If not, I know it's a listing.

[00:01:10] But when we update, notice that instead of setting the method to post, we're setting it to put. And put is a different method that will allow us to update it. Again, we have to say what specific post we wanna update. Pass it the updated the data and only this data will be updated.

[00:01:31] So let's say that we had a category or an exert, it won't override those. And so if all we wanted to do was let's say we only wanted to update the title, then content would not be overridden with our put request. And then we're basically just doing the same thing again, reloading the page and saying hey, this is updated.

[00:01:53] So I'm not going to go through the rest of this with you. Again, we're hitting really perfect timing for what we set out to cover on all of this. And it's not bad to have a few exercises. And because you already did this in WordPress as an actual theme, it's just redundant to do it in a separate environment.

[00:02:14] But it is a helpful code to have and a good practice.