WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp delete post

Description

Removes or trashes a post, attachment, or page.

When the post or page is trashed, all comments tied to the post is also moved to trash. The post meta fields and relationships between the post and taxonomy terms are saved until the post is deleted permanently (eg. emptying the trash or setting $force_delete to true).

When the post or page is permanently deleted, everything that is tied to it is deleted also. This includes comments, post meta fields, and relationships between the post and taxonomy terms.

Usage

 <?php wp_delete_post$postid$force_delete ); ?>

Parameters

$postid
(integer) (optional) Post ID.
Default: 0
$force_delete
(bool) (optional) Whether to bypass trash and force deletion (added in WordPress 2.9).
Default: false

Return Values

(mixed) 
The post object (if it was deleted or moved to the trash successfully) or false (failure). If the post was moved to the trash, $post represents its new state; if it was deleted, $post represents its state before deletion.

Examples

Delete Post

Deleting WP default post "Hello World" which ID is '1'.

 <?php wp_delete_post(1); ?> 

Notes

  • wp_delete_post() automatically reverts to wp_trash_post() if $force_delete is false, the post_type of $postid is page or post, $postid is not already in the trash and if that trash feature enabled (which it it is by default).
  • Uses: do_action() on 'delete_post' before deletion unless post type is 'attachment'.
  • Uses: do_action() on 'deleted_post' after deletion unless post type is attachment.
  • Uses: wp_delete_attachment() if post type is attachment.
  • Uses global $wpdb: (object) wpdb
  • Uses global $wp_rewrite: (object) WP_Rewrite

Change Log

Since: 1.0.0

Source File

wp_delete_post() is located in wp-includes/post.php.

Related

Post, Page, Attachment and Bookmarks Functions: wp_trash_post(), wp_update_post(), wp_delete_attachment(), wp_insert_attachment(), wp_insert_post()

See also index of Function Reference and index of Template Tags.