On this page
wp_dashboard_recent_posts( array $args ): bool
Generates Publishing Soon and Recently Published sections.
Parameters
$argsarray Required-
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
bool False if no posts were found. True otherwise.
Source
File: wp-admin/includes/dashboard.php. View all references
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' => false,
'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;
}
Hooks
- apply_filters( 'dashboard_recent_posts_query_args',
array $query_args ) -
Filters the query arguments used for the Recent Posts widget.
Related
Uses
| Uses | Description |
|---|---|
| current_datetime() wp-includes/functions.php | Retrieves the current time as an object using the site’s timezone. |
| _draft_or_post_title() wp-admin/includes/template.php | Gets the post title. |
| get_the_time() wp-includes/general-template.php | Retrieves the time at which the post was written. |
| WP_Query::__construct() wp-includes/class-wp-query.php | Constructor. |
| wp_reset_postdata() wp-includes/query.php | After looping through a separate query, this function restores the $post global to the current post in the main query. |
| date_i18n() wp-includes/functions.php | Retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds. |
| current_time() wp-includes/functions.php | Retrieves the current time based on specified type. |
| get_edit_post_link() wp-includes/link-template.php | Retrieves the edit post link for post. |
| get_the_ID() wp-includes/post-template.php | Retrieves the ID of the current item in the WordPress Loop. |
| current_user_can() wp-includes/capabilities.php | Returns whether the current user has the specified capability. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| _x() wp-includes/l10n.php | Retrieves translated string with gettext context. |
| esc_attr() wp-includes/formatting.php | Escaping for HTML attributes. |
| get_permalink() wp-includes/link-template.php | Retrieves the full permalink for the current post or post ID. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| wp_dashboard_site_activity() wp-admin/includes/dashboard.php | Callback function for Activity widget. |
Changelog
| Version | Description |
|---|---|
| 3.8.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_dashboard_recent_posts