wp_dashboard_recent_posts( array $args ): bool
- Since
- 3.8.0
- Source
wp-admin/includes/dashboard.php:977
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
$argsarray- An array of query and display arguments.
$maxintNumber of posts to display.$statusstringPost status.$orderstringDesignates ascending ('ASC') or descending ('DESC') order.$titlestringSection title.$idstringThe container id.
Return value
bool- False if no posts were found. True otherwise.
Performance profile
How much work a call to wp_dashboard_recent_posts() 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
- 31–66
- Plugin surface
- 1 hook
- Called by
- 1
Reaches the database via get_post().
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 165.
Third-party callbacks on 'dashboard_recent_posts_query_args' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()one call below wp_dashboard_recent_posts() - querycontent query
get_post()one call below wp_dashboard_recent_posts()
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 · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_dashboard_recent_posts() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!->have_posts() | 31–32 | apply_filters(), post(), ->have_posts() |
| always | 65–66 | apply_filters(), post(), ->have_posts(), current_time(), current_datetime(), ->modify(), ->format(), current_time(), ->have_posts(), wp_reset_postdata() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 163 | 31–66 | 7 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 165 | 31–66 | 7 | |
| 8.4 | 165 | 31–66 | 7 | |
| 8.3 | 165 | 31–66 | 7 | |
| 8.2 | 165 | 31–66 | 7 | |
| 8.1 | 165 | 31–66 | 7 | |
| 7.4 | 165 | 31–66 | 7 |
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.
Hooks and filters fired · 1
One hook fires while wp_dashboard_recent_posts() runs, in this order:
- apply_filters( dashboard_recent_posts_query_args )filterline 996 (+19 into the body)
Filters the query arguments used for the Recent Posts widget.
Uses · 17
- apply_filters()Calls the callback functions that have been added to a filter hook.
- current_time()Retrieves the current time based on specified type.
- current_datetime()Retrieves the current time as an object using the site's timezone.
- get_the_time()Retrieves the time at which the post was written.
- __()Retrieves the translation of $text.
- date_i18n()Retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds.
- current_user_can()Returns whether the current user has the specified capability.
- get_the_ID()Retrieves the ID of the current item in the WordPress Loop.
- get_edit_post_link()Retrieves the edit post link for post.
- get_permalink()Retrieves the full permalink for the current post or post ID.
- _draft_or_post_title()Gets the post title.
- _x()Retrieves translated string with gettext context.
Show all 17
- esc_attr()Escaping for HTML attributes.
- wp_reset_postdata()After looping through a separate query, this function restores the $post global to the current post in the main query.
- WP_Query::__construct()Constructor.
- current_datetime()->modify('+1 day')::format()
- current_datetime()::modify()
Used by · 1
- wp_dashboard_site_activity()Outputs the Activity widget.
Source code
function wp_dashboard_recent_posts( $args ) { $query_args = array( 'post_type' => 'post', 'post_status' => $args['status'], 'orderby' => 'date', 'order' => $args['order'], 'posts_per_page' => (int) $args['max'], 'no_found_rows' => true, 'cache_results' => true, 'perm' => ( 'future' === $args['status'] ) ? 'editable' : 'readable', ); /** * Filters the query arguments used for the Recent Posts widget. * * @since 4.2.0 * * @param array $query_args The arguments passed to WP_Query to produce the list of posts. */ $query_args = apply_filters( 'dashboard_recent_posts_query_args', $query_args ); $posts = new WP_Query( $query_args ); if ( $posts->have_posts() ) { echo '<div id="' . $args['id'] . '" class="activity-block">'; echo '<h3>' . $args['title'] . '</h3>'; echo '<ul>'; $today = current_time( 'Y-m-d' ); $tomorrow = current_datetime()->modify( '+1 day' )->format( 'Y-m-d' ); $year = current_time( 'Y' ); while ( $posts->have_posts() ) { $posts->the_post(); $time = get_the_time( 'U' ); if ( gmdate( 'Y-m-d', $time ) === $today ) { $relative = __( 'Today' ); } elseif ( gmdate( 'Y-m-d', $time ) === $tomorrow ) { $relative = __( 'Tomorrow' ); } elseif ( gmdate( 'Y', $time ) !== $year ) { /* translators: Date and time format for recent posts on the dashboard, from a different calendar year, see https://www.php.net/manual/datetime.format.php */ $relative = date_i18n( __( 'M jS Y' ), $time ); } else { /* translators: Date and time format for recent posts on the dashboard, see https://www.php.net/manual/datetime.format.php */ $relative = date_i18n( __( 'M jS' ), $time ); } // Use the post edit link for those who can edit, the permalink otherwise. $recent_post_link = current_user_can( 'edit_post', get_the_ID() ) ? get_edit_post_link() : get_permalink(); $draft_or_post_title = _draft_or_post_title(); printf( '<li><span>%1$s</span> <a href="%2$s" aria-label="%3$s">%4$s</a></li>', /* translators: 1: Relative date, 2: Time. */ sprintf( _x( '%1$s, %2$s', 'dashboard' ), $relative, get_the_time() ), $recent_post_link, /* translators: %s: Post title. */ esc_attr( sprintf( __( 'Edit “%s”' ), $draft_or_post_title ) ), $draft_or_post_title ); } echo '</ul>'; echo '</div>'; } else { return false; } wp_reset_postdata(); 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.7.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.