wp_untrash_post_comments

函数
wp_untrash_post_comments ( $post = null )
参数
  • (int|WP_Post|null) $post Optional. Post ID or post object. Defaults to global $post.
    Required:
    Default: null
返回值
  • (true|void)
定义位置
相关方法
wp_trash_post_commentswp_untrash_commentwp_trash_commentwp_untrash_postwp_count_comments
引入
2.9.0
弃用
-

wp_untrash_post_comments: 这个函数将所有与弃用的文章相关的评论的状态从”垃圾”改为”批准”。该文章是由它的ID指定的,该函数更新数据库以反映状态的变化。

从回收站中恢复一个文章的评论。

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

	$post = get_post( $post );

	if ( ! $post ) {
		return;
	}

	$post_id = $post->ID;

	$statuses = get_post_meta( $post_id, '_wp_trash_meta_comments_status', true );

	if ( ! $statuses ) {
		return true;
	}

	/**
	 * Fires before comments are restored for a post from the Trash.
	 *
	 * @since 2.9.0
	 *
	 * @param int $post_id Post ID.
	 */
	do_action( 'untrash_post_comments', $post_id );

	// Restore each comment to its original status.
	$group_by_status = array();
	foreach ( $statuses as $comment_id => $comment_status ) {
		$group_by_status[ $comment_status ][] = $comment_id;
	}

	foreach ( $group_by_status as $status => $comments ) {
		// Sanity check. This shouldn't happen.
		if ( 'post-trashed' === $status ) {
			$status = '0';
		}
		$comments_in = implode( ', ', array_map( 'intval', $comments ) );
		$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) );
	}

	clean_comment_cache( array_keys( $statuses ) );

	delete_post_meta( $post_id, '_wp_trash_meta_comments_status' );

	/**
	 * Fires after comments are restored for a post from the Trash.
	 *
	 * @since 2.9.0
	 *
	 * @param int $post_id Post ID.
	 */
	do_action( 'untrashed_post_comments', $post_id );
}

常见问题

FAQs
查看更多 >