返回介绍

wp_delete_post()

发布于 2017-09-11 11:51:17 字数 9734 浏览 978 评论 0 收藏 0

wp_delete_post( int $postid,  bool $force_delete = false )

Trash or delete a post or page.


description

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.


参数

$postid

(int) (Optional) Post ID. Default 0.

$force_delete

(bool) (Optional) Whether to bypass trash and force deletion.

Default value: false


返回值

(array|false|WP_Post) False on failure.


源代码

File: wp-includes/post.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 );

	/**
	 * Filters whether a post deletion should take place.
	 *
	 * @since 4.4.0
	 *
	 * @param bool    $delete       Whether to go forward with deletion.
	 * @param WP_Post $post         Post object.
	 * @param bool    $force_delete Whether to bypass the trash.
	 */
	$check = apply_filters( 'pre_delete_post', null, $post, $force_delete );
	if ( null !== $check ) {
		return $check;
	}

	/**
	 * Fires before a post is deleted, at the start of wp_delete_post().
	 *
	 * @since 3.2.0
	 *
	 * @see wp_delete_post()
	 *
	 * @param int $postid Post ID.
	 */
	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 );
		if ( $children ) {
			$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) );
		}
	}

	// 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' ) );

	wp_defer_comment_counting( true );

	$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 );
	}

	wp_defer_comment_counting( false );

	$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 );

	/**
	 * Fires immediately before a post is deleted from the database.
	 *
	 * @since 1.2.0
	 *
	 * @param int $postid Post ID.
	 */
	do_action( 'delete_post', $postid );
	$result = $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) );
	if ( ! $result ) {
		return false;
	}

	/**
	 * Fires immediately after a post is deleted from the database.
	 *
	 * @since 2.2.0
	 *
	 * @param int $postid Post ID.
	 */
	do_action( 'deleted_post', $postid );

	clean_post_cache( $post );

	if ( is_post_type_hierarchical( $post->post_type ) && $children ) {
		foreach ( $children as $child )
			clean_post_cache( $child );
	}

	wp_clear_scheduled_hook('publish_future_post', array( $postid ) );

	/**
	 * Fires after a post is deleted, at the conclusion of wp_delete_post().
	 *
	 * @since 3.2.0
	 *
	 * @see wp_delete_post()
	 *
	 * @param int $postid Post ID.
	 */
	do_action( 'after_delete_post', $postid );

	return $post;
}

更新日志

Versiondescription
1.0.0Introduced.

相关函数

Uses

  • wp-includes/post.php: pre_delete_post
  • wp-includes/cron.php: wp_clear_scheduled_hook()
  • wp-includes/taxonomy.php: wp_delete_object_term_relationships()
  • wp-includes/taxonomy.php: get_object_taxonomies()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/plugin.php: do_action()
  • wp-includes/post.php: clean_post_cache()
  • wp-includes/post.php: wp_delete_attachment()
  • wp-includes/post.php: wp_trash_post()
  • wp-includes/post.php: before_delete_post
  • wp-includes/post.php: delete_post
  • wp-includes/post.php: deleted_post
  • wp-includes/post.php: after_delete_post
  • wp-includes/post.php: delete_post_meta()
  • wp-includes/post.php: get_post_status()
  • wp-includes/post.php: is_post_type_hierarchical()
  • wp-includes/revision.php: wp_delete_post_revision()
  • wp-includes/wp-db.php: wpdb::get_row()
  • wp-includes/wp-db.php: wpdb::get_results()
  • wp-includes/wp-db.php: wpdb::update()
  • wp-includes/wp-db.php: wpdb::get_col()
  • wp-includes/wp-db.php: wpdb::delete()
  • wp-includes/wp-db.php: wpdb::prepare()
  • wp-includes/comment.php: wp_defer_comment_counting()
  • wp-includes/comment.php: wp_delete_comment()
  • wp-includes/meta.php: delete_metadata_by_mid()
  • Show 21 more uses Hide more uses

Used By

  • wp-includes/nav-menu.php: _wp_delete_customize_changeset_dependent_auto_drafts()
  • wp-includes/theme.php: _wp_customize_publish_changeset()
  • wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php: WP_REST_Revisions_Controller::delete_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::delete_item()
  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php: WP_Customize_Nav_Menu_Item_Setting::update()
  • wp-admin/includes/ms.php: wpmu_delete_user()
  • wp-admin/includes/user.php: wp_delete_user()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_attachment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_post()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_page()
  • wp-admin/includes/nav-menu.php: _wp_delete_orphaned_draft_menu_items()
  • wp-admin/includes/nav-menu.php: wp_nav_menu_update_menu_items()
  • wp-includes/functions.php: wp_scheduled_delete()
  • wp-includes/post.php: wp_delete_auto_drafts()
  • wp-includes/post.php: wp_trash_post()
  • wp-includes/revision.php: wp_delete_post_revision()
  • wp-includes/nav-menu.php: _wp_delete_post_menu_item()
  • wp-includes/nav-menu.php: _wp_delete_tax_menu_item()
  • wp-includes/nav-menu.php: wp_delete_nav_menu()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::blogger_deletePost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_deletePage()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_deletePost()
  • Show 17 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note Contributed by Said El Bakkali
    
    /**
     * Deletes all posts from "products" custom post type.
     */
    function wpdocs_delete_all_products() {
        $myproducts = get_pages( array( 'post_type' => 'products') );
    
        foreach ( $myproducts as $myproduct ) {
            // Delete all products.
            wp_delete_post( $myproduct->ID, true); // Set to False if you want to send them to Trash.
        } 
    }
    add_action( 'init', 'wpdocs_delete_all_products' );
    
  2. Delete Post
    Deleting the WP default post “Hello World” which has ID 1.

    
     <?php wp_delete_post(1); ?> 
    
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文