wppaste
WordPress

wp_dashboard_recent_drafts( WP_Post[]|false $drafts = false )

Since
2.7.0
Source
wp-admin/includes/dashboard.php:623
Show recent drafts of the user on the dashboard.

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

Reaches the database via get_posts().

Scaling
Scales with input

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

Instructions
22–62

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

Plugin surface
1 hook

Third-party callbacks on 'dashboard_recent_drafts_query_args' run inside this call, and their cost is not bounded by anything here.

Called by
1

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

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_posts()called directly
  • optionoption read or writeget_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.

WhenInstructionsCalls it makes
always22get_current_user_id(), apply_filters(), get_posts()
always28–29__(), _x(), array_slice()
always42–43admin_url(), esc_url(), __(), printf(), __(), _x(), array_slice()
always47–48get_current_user_id(), apply_filters(), get_posts(), __(), _x(), array_slice()
always61–62get_current_user_id(), apply_filters(), get_posts(), admin_url(), esc_url(), __(), printf(), __(), _x(), array_slice()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12922–6365 more instructions than PHP 8.5
8.512422–626
8.412422–626
8.312422–626
8.212422–626
8.112422–626
7.412422–626

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:

  1. 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

Show all 13

Used by · 1

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 &#8220;%s&#8221;' ), $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.

  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.

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.