wp_dashboard_recent_comments( int $total_items = 5 ): bool
- Since
- 3.8.0
- Source
wp-admin/includes/dashboard.php:1064
Compatibility
- WordPress
- since 3.8.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
$total_itemsintoptional- Number of comments to query. Default 5.Default:
5
Return value
bool- False if no comments were found. True otherwise.
Performance profile
How much work a call to wp_dashboard_recent_comments() 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
- 19–88
- Plugin surface
- None
- Called by
- 1
Reaches the database via get_comments().
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 102.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_comments()called directly - hookthird-party callbacks
apply_filters()one call below wp_dashboard_recent_comments() - optionoption read or write
get_option()one call below wp_dashboard_recent_comments()
Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_dashboard_recent_comments() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 19–34 | current_user_can(), get_comments() |
current_user_can() && !empty($possible) && is_array($possible) && !$possible && $comments_count === false | 37–39 | current_user_can(), get_comments(), current_user_can() |
!current_user_can() | 43–59 | current_user_can(), get_comments(), __(), current_user_can(), wp_comment_reply(), wp_comment_trashnotice() |
!empty($possible) && is_array($possible) && !$possible && !post_password_required() && $comments_count === false | 50–52 | current_user_can(), get_comments(), current_user_can(), post_password_required(), current_user_can() |
current_user_can() | 54–70 | current_user_can(), get_comments(), __(), current_user_can(), __(), _get_list_table(), ->views(), wp_comment_reply(), wp_comment_trashnotice() |
!empty($possible) && is_array($possible) && !$possible && $comments_count === false | 61–64 | current_user_can(), get_comments(), current_user_can(), __(), current_user_can(), wp_comment_reply(), wp_comment_trashnotice() |
current_user_can() && !empty($possible) && is_array($possible) && !$possible && $comments_count === false | 72–75 | current_user_can(), get_comments(), current_user_can(), __(), current_user_can(), __(), _get_list_table(), ->views(), wp_comment_reply(), wp_comment_trashnotice() |
!empty($possible) && is_array($possible) && !$possible && !post_password_required() && $comments_count === false | 74–77 | current_user_can(), get_comments(), current_user_can(), post_password_required(), current_user_can(), __(), current_user_can(), wp_comment_reply(), wp_comment_trashnotice() |
!empty($possible) && is_array($possible) && !$possible && !post_password_required() && $comments_count === false | 85–88 | current_user_can(), get_comments(), current_user_can(), post_password_required(), current_user_can(), __(), current_user_can(), __(), _get_list_table(), ->views(), wp_comment_reply(), wp_comment_trashnotice() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 102 instructions, 19–88 executed per call, 14 branches. The work does not change between versions.
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 · 9
- current_user_can()Returns whether the current user has the specified capability.
- get_comments()Retrieves a list of comments.
- post_password_required()Determines whether the post requires password and whether a correct password has been provided.
- __()Retrieves the translation of $text.
- _wp_dashboard_recent_comments_row()Outputs a row for the Recent Comments widget.
- _get_list_table()Fetches an instance of a WP_List_Table class.
- wp_comment_reply()Outputs the in-line comment reply-to form in the Comments list table.
- wp_comment_trashnotice()Outputs 'undo move to Trash' text for comments.
- _get_list_table('WP_Comments_List_Table')::views()
Used by · 1
- wp_dashboard_site_activity()Outputs the Activity widget.
Source code
function wp_dashboard_recent_comments( $total_items = 5 ) { // Select all comment types and filter out spam later for better query performance. $comments = array(); $comments_query = array( 'number' => $total_items * 5, 'offset' => 0, ); if ( ! current_user_can( 'edit_posts' ) ) { $comments_query['status'] = 'approve'; } $comments_count = 0; do { $possible = get_comments( $comments_query ); if ( empty( $possible ) || ! is_array( $possible ) ) { break; } foreach ( $possible as $comment ) { if ( ! current_user_can( 'edit_post', $comment->comment_post_ID ) && ( post_password_required( $comment->comment_post_ID ) || ! current_user_can( 'read_post', $comment->comment_post_ID ) ) ) { // The user has no access to the post and thus cannot see the comments. continue; } $comments[] = $comment; $comments_count = count( $comments ); if ( $comments_count === $total_items ) { break 2; } } $comments_query['offset'] += $comments_query['number']; $comments_query['number'] = $total_items * 10; } while ( $comments_count < $total_items ); if ( $comments ) { echo '<div id="latest-comments" class="activity-block table-view-list">'; echo '<h3>' . __( 'Recent Comments' ) . '</h3>'; echo '<ul id="the-comment-list" data-wp-lists="list:comment">'; foreach ( $comments as $comment ) { _wp_dashboard_recent_comments_row( $comment ); } echo '</ul>'; if ( current_user_can( 'edit_posts' ) ) { echo '<h3 class="screen-reader-text">' . /* translators: Hidden accessibility text. */ __( 'View more comments' ) . '</h3>'; _get_list_table( 'WP_Comments_List_Table' )->views(); } wp_comment_reply( -1, false, 'dashboard', false ); wp_comment_trashnotice(); echo '</div>'; } else { return false; } return true;}Changelog
Introduced in 3.8.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/dashboard.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.