get_pending_comments_num( int|int[] $post_id ): int|int[]
- Since
- 2.3.0, 6.9.0
- Source
wp-admin/includes/comment.php:148
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
- Scaling
- Scales with input
- Instructions
- 30–39
- Plugin surface
- None
- Called by
- 5
Reaches the database via ->get_results().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 62.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 30–39 | array_map(), ->get_results() |
!empty($pending) | 36–38 | array_map(), ->get_results(), absint() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 62 | 30–39 | 8 | |
| 8.5 | 62 | 30–39 | 8 | |
| 8.4 | 62 | 30–39 | 8 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 65 | 33–42 | 8 | |
| 8.2 | 65 | 33–42 | 8 | |
| 8.1 | 65 | 33–42 | 8 | |
| 7.4 | 65 | 33–42 | 8 |
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
- WP_Comments_List_Table::column_response()
- WP_Comments_List_Table::prepare_items()
- WP_Media_List_Table::column_comments()Handles the comments column output.
- WP_Media_List_Table::display_rows()Generates the list table rows.
- WP_Posts_List_Table::_display_rows()
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.