wppaste
WordPress

get_pending_comments_num( int|int[] $post_id ): int|int[]

Since
2.3.0, 6.9.0
Source
wp-admin/includes/comment.php:148
Gets the number of pending comments on a post or posts.

Compatibility

WordPress
since 6.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

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

Return value

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

Performance profile

How much work a call to get_pending_comments_num() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_results().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
30–39

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 62.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • sqldatabase query->get_results()called directly

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_pending_comments_num() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always30–39array_map(), ->get_results()
!empty($pending)36–38array_map(), ->get_results(), absint()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev6230–398
8.56230–398
8.46230–3983 fewer instructions than PHP 8.3
8.36533–428
8.26533–428
8.16533–428
7.46533–428

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 1

  • absint()Converts a value to non-negative integer.

Used by · 5

Source code

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

Introduced in 2.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

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

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-admin/includes/comment.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.