At times, you may need to delete a post using a WordPress plugin. For example, if you had a calendar system that created posts for events, and the event was canceled or happened in the past, the ability to automatically delete posts can be quite useful. In this article, we will show you how to easily delete WordPress posts using the wp_delete_post() function.
For more information on creating WordPress plugins, review our article on creating your first WordPress plugin
<?php wp_delete_post( $postid, $force_delete ); ?>
$postid: This parameter allows you to define the post that will be deleted and accepts the post ID. For example, if you want to delete the post with ID 5, set the parameter to 5.
$force_delete: This optional parameter allows you to bypass the trash and permanently delete the post. It is accepts a boolean (true of false) and defaults to false.
<?php wp_delete_post(5); ?>
In this example, we are simply calling the wp_delete_post() function to delete the post with ID 5.
For more information on the wp_delete_post() function, you may review the wp_delete_post() WordPress Codex page.