WordPress函数:wp_trash_post_comments 移动文章评论到垃圾站

江河/ 2023年11月29日/ WordPress/ 浏览 411

函数原型:


wp_trash_post_comments( int|WP_Post|null $post = null ): mixed|void


移动文章评论到垃圾站。


参数说明:


$post,文章或文章ID,如果空,则默认使用全局 $post 指向的文章。


函数源码:


function wp_trash_post_comments( $post = null ) {
	global $wpdb;

	$post = get_post( $post );

	if ( ! $post ) {
		return;
	}

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

	do_action( 'trashed_post_comments', $post_id, $statuses );

	return $result;
}


包含钩子:


do_action( 'trashed_post_comments', int $post_id, array $statuses )

do_action( 'trash_post_comments', int $post_id )


使用举例:


wp_trash_post_comments(1);


删除文章ID为1的文章的评论。


发表评论

暂无评论,抢个沙发...

客服 工单