使用 wp_delete_post 足够吗?

huangapple go评论67阅读模式
英文:

Is using wp_delete_post enough?

问题

I would like to know if just using wp_delete_post($post_id, true); is enough to delete everything in DB that is tied to the post, skipping the trash.

根据文档,是的,但互联网上有一些答案提到也要使用delete_post_meta,比如这样:

// 删除所有文章元数据
foreach (get_post_meta($post_id) as $key => $val) {
    delete_post_meta($post_id, $key);
}

wp_delete_post($post_id, true);

最佳实践是什么?

英文:

I would like to know if just using wp_delete_post($post_id, true); is enough to delete everything in DB that is tied to the post, skipping the trash.

According to the documentation it is, but there are several answers on the internet mentioning to use delete_post_meta also, like this:

// Delete all Post Meta
foreach (get_post_meta($post_id) as $key => $val)  {
    delete_post_meta($post_id, $key);
}

wp_delete_post($post_id, true);

What's the best practice?

答案1

得分: 1

根据您提供的链接页面上的信息,wp_delete_post($post_id, true); 删除与帖子关联的所有元数据和评论。它强制删除帖子,因此绕过了垃圾箱。对于您的问题,这已经足够。

代码引用(wp-includes/post.php:3438-3448):

$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $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 );
}

如果您只想删除帖子元数据,请不要使用wp_delete_post(),而是使用delete_post_meta()

英文:

According to the source on the page you linked, wp_delete_post($post_id, true); deletes all the metadata and comments linked to the post. It force deletes the post and does therefore bypass the trash. As an answer to your question, it is enough.

Reference in code (wp-includes/post.php:3438-3448):

$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $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 );
}

If you would only want to delete post meta, please refrain of using wp_delete_post() and do instead use delete_post_meta().

答案2

得分: 0

使用 wp_delete_post($post_id, true) 会从数据库中删除与帖子相关的所有信息,包括附件和帖子元数据,并绕过回收站,导致立即删除。但如果您想删除与帖子关联的帖子元数据,您还应该使用 delete_post_meta() 函数,以补充 wp_delete_post() 函数。

英文:

YES. Using wp_delete_post($post_id, true) deletes all post-related information from the database, including attachments and post meta data, and bypasses the trash, resulting in immediate deletion. But, if you want to delete post meta data associated with the post, you should use delete_post_meta() function in addition to wp_delete_post() function.

huangapple
  • 本文由 发表于 2023年2月26日 22:46:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定