将帖子的评论移至垃圾箱。

原型

wp_trash_post_comments( int|WP_Post|null $post = null )

参数

$post (int|WP_Post|null) (Optional)  帖子ID或帖子对象。默认为全球$ post。

返回值

(mixed|void)  失败时是假的。

源文件

路径:wp-includes/post.php

<?php
...
function wp_trash_post_comments( $post = null ) {
	global $wpdb;
	$post = get_post($post);
	if ( empty($post) )
		return;
	$post_id = $post->ID;
	/**
	 * Fires before comments are sent to the trash.
	 *
	 * @since 2.9.0
	 *
	 * @param int $post_id Post ID.
	 */
	do_action( 'trash_post_comments', $post_id );
	$comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id) );
	if ( empty($comments) )
		return;
	// Cache current status for each comment.
	$statuses = array();
	foreach ( $comments as $comment )
		$statuses[$comment->comment_ID] = $comment->comment_approved;
	add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses);
	// Set status for all comments to post-trashed.
	$result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id));
	clean_comment_cache( array_keys($statuses) );
	/**
	 * Fires after comments are sent to the trash.
	 *
	 * @since 2.9.0
	 *
	 * @param int   $post_id  Post ID.
	 * @param array $statuses Array of comment statuses.
	 */
	do_action( 'trashed_post_comments', $post_id, $statuses );
	return $result;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/wp_trash_post_comments/