wp_dashboard_recent_drafts( WP_Post[]|false $drafts = false )
- Since
- 2.7.0
- Source
wp-admin/includes/dashboard.php:623
Compatibility
- WordPress
- since 2.7.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
$draftsWP_Post[]|falseoptional- Array of posts to display. Default false.Default:
false
Performance profile
How much work a call to wp_dashboard_recent_drafts() 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
- 22–62
- Plugin surface
- 1 hook
- Called by
- 1
Reaches the database via get_posts().
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 124.
Third-party callbacks on 'dashboard_recent_drafts_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 - querycontent query
get_posts()called directly - optionoption read or write
get_option()one call below wp_dashboard_recent_drafts()
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 · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_dashboard_recent_drafts() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 22 | get_current_user_id(), apply_filters(), get_posts() |
| always | 28–29 | __(), _x(), array_slice() |
| always | 42–43 | admin_url(), esc_url(), __(), printf(), __(), _x(), array_slice() |
| always | 47–48 | get_current_user_id(), apply_filters(), get_posts(), __(), _x(), array_slice() |
| always | 61–62 | get_current_user_id(), apply_filters(), get_posts(), admin_url(), esc_url(), __(), printf(), __(), _x(), array_slice() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 129 | 22–63 | 6 | 5 more instructions than PHP 8.5 |
| 8.5 | 124 | 22–62 | 6 | |
| 8.4 | 124 | 22–62 | 6 | |
| 8.3 | 124 | 22–62 | 6 | |
| 8.2 | 124 | 22–62 | 6 | |
| 8.1 | 124 | 22–62 | 6 | |
| 7.4 | 124 | 22–62 | 6 |
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_drafts() runs, in this order:
- apply_filters( dashboard_recent_drafts_query_args )filterline 641 (+18 into the body)
Filters the post query arguments for the 'Recent Drafts' dashboard widget.
Uses · 13
- get_current_user_id()Gets the current user's ID.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_posts()Retrieves an array of the latest posts, or posts matching the given criteria.
- esc_url()Checks and cleans a URL.
- admin_url()Retrieves the URL to the admin area for the current site.
- __()Retrieves the translation of $text.
- _x()Retrieves translated string with gettext context.
- get_edit_post_link()Retrieves the edit post link for post.
- _draft_or_post_title()Gets the post title.
- esc_attr()Escaping for HTML attributes.
- esc_html()Escaping for HTML blocks.
- get_the_time()Retrieves the time of the post.
Show all 13
- wp_trim_words()Trims text to a certain number of words.
Used by · 1
- wp_dashboard_quick_press()Displays the Quick Draft widget.
Source code
function wp_dashboard_recent_drafts( $drafts = false ) { if ( ! $drafts ) { $query_args = array( 'post_type' => 'post', 'post_status' => 'draft', 'author' => get_current_user_id(), 'posts_per_page' => 4, 'orderby' => 'modified', 'order' => 'DESC', ); /** * Filters the post query arguments for the 'Recent Drafts' dashboard widget. * * @since 4.4.0 * * @param array $query_args The query arguments for the 'Recent Drafts' dashboard widget. */ $query_args = apply_filters( 'dashboard_recent_drafts_query_args', $query_args ); $drafts = get_posts( $query_args ); if ( ! $drafts ) { return; } } echo '<div class="drafts">'; if ( count( $drafts ) > 3 ) { printf( '<p class="view-all"><a href="%s">%s</a></p>' . "\n", esc_url( admin_url( 'edit.php?post_status=draft' ) ), __( 'View all drafts' ) ); } echo '<h2 class="hide-if-no-js">' . __( 'Your Recent Drafts' ) . "</h2>\n"; echo '<ul>'; /* translators: Maximum number of words used in a preview of a draft on the dashboard. */ $draft_length = (int) _x( '10', 'draft_length' ); $drafts = array_slice( $drafts, 0, 3 ); foreach ( $drafts as $draft ) { $url = get_edit_post_link( $draft->ID ); $title = _draft_or_post_title( $draft->ID ); echo "<li>\n"; printf( '<div class="draft-title"><a href="%s" aria-label="%s">%s</a><time datetime="%s">%s</time></div>', esc_url( $url ), /* translators: %s: Post title. */ esc_attr( sprintf( __( 'Edit “%s”' ), $title ) ), esc_html( $title ), get_the_time( 'c', $draft ), get_the_time( __( 'F j, Y' ), $draft ) ); $the_content = wp_trim_words( $draft->post_content, $draft_length ); if ( $the_content ) { echo '<p>' . $the_content . '</p>'; } echo "</li>\n"; } echo "</ul>\n"; echo '</div>';}Changelog
Introduced in 2.7.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.