描述
wordpress 删除一篇文章,附件,或者页面。
当在文章或页面使用这个函数的时候,所有依赖这个文章或页面的内容也将被删除。包括评论,自定义字段,和文章与类别的关系
使用方法
- <?php wp_delete_post( $postid, $force_delete ); ?>
参数
$postid
(整形)(可选) 文章 ID.
默认: 0
$force_delete
(布尔型) (可选) 是否绕过回收站箱直接删除 (在 WordPress 2.9 添加).
默认: false
返回值
混合型(mixed)
False on failure and a random wpdb object on success.
例子
删除文章
删除默认文章 “Hello World” 文章 ID 是 ‘1’.
- <?php wp_delete_post(1); ?>
源文件
wp_delete_post() 位于 wp-includes/post.php.
- function wp_delete_post( $postid = 0, $force_delete = false ) {
- global $wpdb, $wp_rewrite;
- 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 ( 'page' == $post->post_type) {
-
-
- 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');
- }
-
- $children_query = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type='page'", $postid);
- $children = $wpdb->get_results($children_query);
- $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'page' ) );
- } else {
- unstick_post($postid);
- }
-
- $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) );
-
- foreach ( $revision_ids as $revision_id )
- wp_delete_post_revision( $revision_id );
-
- $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 ));
- if ( ! emptyempty($comment_ids) ) {
- do_action( 'delete_comment', $comment_ids );
- foreach ( $comment_ids as $comment_id )
- wp_delete_comment( $comment_id, true );
- do_action( 'deleted_comment', $comment_ids );
- }
- $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ));
- if ( !emptyempty($post_meta_ids) ) {
- do_action( 'delete_postmeta', $post_meta_ids );
- $in_post_meta_ids = "'" . implode("', '", $post_meta_ids) . "'";
- $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id IN($in_post_meta_ids)" );
- do_action( 'deleted_postmeta', $post_meta_ids );
- }
- do_action( 'delete_post', $postid );
- $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid ));
- do_action( 'deleted_post', $postid );
- if ( 'page' == $post->post_type ) {
- clean_page_cache($postid);
- foreach ( (array) $children as $child )
- clean_page_cache($child->ID);
- $wp_rewrite->flush_rules(false);
- } else {
- clean_post_cache($postid);
- }
- wp_clear_scheduled_hook('publish_future_post', array( $postid ) );
- do_action('after_delete_post', $postid);
- return $post;
- }