wp_delete_post [ WordPress Function ]
| Parameters: |
|
| Uses: | |
| Returns: |
|
| Defined at: |
|
Trashes or deletes a post or page.
When the post and page is permanently deleted, everything that is tied to it is deleted also. This includes comments, post meta fields, and terms associated with the post.
The post or page is moved to trash instead of permanently deleted unless trash is disabled, item is already in the trash, or $force_delete is true.
Source
<?php
function wp_delete_post( $postid = 0, $force_delete = false ) {
global $wpdb;
if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) )
return $post;
if ( !$force_delete && ( $post->post_type == 'post' || $post->post_type == 'page') && get_post_status( $postid ) != 'trash' && EMPTY_TRASH_DAYS )
return wp_trash_post($postid);
if ( $post->post_type == 'attachment' )
return wp_delete_attachment( $postid, $force_delete );
do_action('before_delete_post', $postid);
delete_post_meta($postid,'_wp_trash_meta_status');
delete_post_meta($postid,'_wp_trash_meta_time');
wp_delete_object_term_relationships($postid, get_object_taxonomies($post->post_type));
$parent_data = array( 'post_parent' => $post->post_parent );
$parent_where = array( 'post_parent' => $postid );
if ( is_post_type_hierarchical( $post->post_type ) ) {
// Point children of this page to its parent, also clean the cache of affected children
$children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type );
$children = $wpdb->get_results( $children_query );
$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) );
}
if ( 'page' == $post->post_type) {
// if the page is defined in option page_on_front or post_for_posts,
// adjust the corresponding options
if ( get_option('page_on_front') == $postid ) {
update_option('show_on_front', 'posts');
delete_option('page_on_front');
}
if ( get_option('page_for_posts') == $postid ) {
delete_option('page_for_posts');
}
} else {
unstick_post($postid);
}
// Do raw query. wp_get_post_revisions() is filtered
$revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) );
// Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up.
foreach ( $revision_ids as $revision_id )
wp_delete_post_revision( $revision_id );
// Point all attachments to this post up one level
$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );
$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ));
foreach ( $comment_ids as $comment_id )
wp_delete_comment( $comment_id, true );
$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ));
foreach ( $post_meta_ids as $mid )
delete_metadata_by_mid( 'post', $mid );
do_action( 'delete_post', $postid );
$wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) );
do_action( 'deleted_post', $postid );
clean_post_cache( $post );
if ( is_post_type_hierarchical( $post->post_type ) ) {
foreach ( (array) $children as $child )
clean_post_cache( $child );
}
wp_clear_scheduled_hook('publish_future_post', array( $postid ) );
do_action('after_delete_post', $postid);
return $post;
}
?>
Examples [ wp-snippets.com ]
Top Google Search Results
- Function Reference/wp delete post « WordPress Codex
Description. Removes a post, attachment, or page. When the post and page goes , everything that is tied to it is deleted also. This includes comments, post meta ...
codex.wordpress.org - WordPress › Support » Tags — wp_delete_post
Register · WordPress › Support » wp_delete_post ... wp_delete_post does not delete attachments? ... wpdb query for wp_delete_post, 1, MolaMola, 10 months ...
wordpress.org - wp_delete_post | A HitchHackers guide through WordPress
Feb 12, 2011 ... function wp_delete_post( $postid = 0, $force_delete = false ) { global ... $postid ) ) ; // Use wp_delete_post (via wp_delete_post_revision) again.
hitchhackerguide.com - wp_delete_post (WordPress Function) - WPSeek.com
WordPress lookup for wp_delete_post, a WordPress Function. wpseek.com is a WordPress-centric search tool for developers and theme authors.
wpseek.com
User discussions [ wordpress.org ]
- robmuzo on "using wp_delete_post to delete all posts from a custom post type"
- Frumph on "using wp_delete_post to delete all posts from a custom post type"
- robmuzo on "using wp_delete_post to delete all posts from a custom post type"
- robmuzo on "using wp_delete_post to delete all posts from a custom post type"
- scrollpost on "Delete old posts programmatically"
- popper on "wp_delete_post does not delete attachments?"
- salvatore.formisano on "wp_delete_post does not delete attachments?"
- popper on "wp_delete_post does not delete attachments?"
- salvatore.formisano on "wp_delete_post does not delete attachments?"
- MolaMola on "wpdb query for wp_delete_post"