wppaste
WordPress

wp_dashboard_recent_posts( array $args ): bool

Since
3.8.0
Source
wp-admin/includes/dashboard.php:990
Generates Publishing Soon and Recently Published sections.

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.
  • $maxint

    Number of posts to display.
  • $statusstring

    Post status.
  • $orderstring

    Designates ascending ('ASC') or descending ('DESC') order.
  • $titlestring

    Section title.
  • $idstring

    The 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

Reaches the database via get_post().

Scaling
Scales with input

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

Instructions
31–66

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

Plugin surface
1 hook

Third-party callbacks on 'dashboard_recent_posts_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
  • optionoption read or writeget_option()one call below wp_dashboard_recent_posts()
  • querycontent queryget_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.

WhenInstructionsCalls it makes
!->have_posts()31–32apply_filters(), post(), ->have_posts()
always65–66apply_filters(), post(), ->have_posts(), current_time(), current_datetime(), ->modify(), ->format(), current_time(), ->have_posts(), wp_reset_postdata()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev17331–6682 fewer instructions than PHP 8.5
8.517531–668
8.417531–668
8.317531–668
8.217531–668
8.117531–668
7.417531–668

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:

  1. apply_filters( dashboard_recent_posts_query_args )filterline 1009 (+19 into the body)

    Filters the query arguments used for the Recent Posts widget.

Uses · 18

Show all 18
  • _x()Retrieves translated string with gettext context.
  • 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

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 ( ! is_int( $time ) ) {				/* 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 */				$date = get_the_date( __( 'M jS Y' ) );			} elseif ( gmdate( 'Y-m-d', $time ) === $today ) {				$date = __( 'Today' );			} elseif ( gmdate( 'Y-m-d', $time ) === $tomorrow ) {				$date = __( '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 */				$date = 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 */				$date = 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' ), $date, get_the_time() ),				$recent_post_link,				/* translators: %s: Post title. */				esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $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.

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