函数文档

get_pending_comments_num()

💡 云策文档标注

概述

get_pending_comments_num() 函数用于获取一个或多个文章的待审核评论数量。它接受单个文章 ID 或文章 ID 数组作为参数,并返回相应的整数或数组。

关键要点

  • 参数 $post_id 可以是 int 或 int[],用于指定单个文章 ID 或文章 ID 数组。
  • 返回值根据参数类型:单个 ID 返回 int 类型待审核评论数;数组则返回以文章 ID 为键的 int[] 数组。
  • 函数内部使用 $wpdb 查询数据库,筛选 comment_approved 为 '0' 且 comment_type 不为 'note' 的评论。
  • 在 WordPress 6.9.0 版本中,排除了 'note' 评论类型,以避免计数干扰。

代码示例

function get_pending_comments_num( $post_id ) {
	global $wpdb;

	$single = false;
	if ( ! is_array( $post_id ) ) {
		$post_id_array = (array) $post_id;
		$single        = true;
	} else {
		$post_id_array = $post_id;
	}
	$post_id_array = array_map( 'intval', $post_id_array );
	$post_id_in    = "'" . implode( "', '", $post_id_array ) . "'";

	$pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' AND comment_type != 'note' GROUP BY comment_post_ID", ARRAY_A );

	if ( $single ) {
		if ( empty( $pending ) ) {
			return 0;
		} else {
			return absint( $pending[0]['num_comments'] );
		}
	}

	$pending_keyed = array();

	// Default to zero pending for all posts in request.
	foreach ( $post_id_array as $id ) {
		$pending_keyed[ $id ] = 0;
	}

	if ( ! empty( $pending ) ) {
		foreach ( $pending as $pend ) {
			$pending_keyed[ $pend['comment_post_ID'] ] = absint( $pend['num_comments'] );
		}
	}

	return $pending_keyed;
}

注意事项

  • 函数依赖于 WordPress 数据库对象 $wpdb 执行 SQL 查询,确保在调用前已正确初始化。
  • 返回的数组默认包含所有请求文章 ID 的键,即使没有待审核评论也会设置为 0,便于统一处理。
  • 使用 absint() 函数确保返回值为非负整数,增强数据安全性。

📄 原文内容

Gets the number of pending comments on a post or posts.

Parameters

$post_idint|int[]required
Either a single Post ID or an array of Post IDs

Return

int|int[] Either a single Posts pending comments as an int or an array of ints keyed on the Post IDs

Source

function get_pending_comments_num( $post_id ) {
	global $wpdb;

	$single = false;
	if ( ! is_array( $post_id ) ) {
		$post_id_array = (array) $post_id;
		$single        = true;
	} else {
		$post_id_array = $post_id;
	}
	$post_id_array = array_map( 'intval', $post_id_array );
	$post_id_in    = "'" . implode( "', '", $post_id_array ) . "'";

	$pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' AND comment_type != 'note' GROUP BY comment_post_ID", ARRAY_A );

	if ( $single ) {
		if ( empty( $pending ) ) {
			return 0;
		} else {
			return absint( $pending[0]['num_comments'] );
		}
	}

	$pending_keyed = array();

	// Default to zero pending for all posts in request.
	foreach ( $post_id_array as $id ) {
		$pending_keyed[ $id ] = 0;
	}

	if ( ! empty( $pending ) ) {
		foreach ( $pending as $pend ) {
			$pending_keyed[ $pend['comment_post_ID'] ] = absint( $pend['num_comments'] );
		}
	}

	return $pending_keyed;
}

Changelog

Version Description
6.9.0 Exclude the 'note' comment type from the count.
2.3.0 Introduced.