wp_dashboard_recent_posts

Generates Publishing Soon and Recently Published sections.

Description

wp_dashboard_recent_posts( (array) $args ); 

Parameters (1)

0. $args (array)
An array of query and display arguments.

Options

  • max (int) => 0

    Number of posts to display.

  • status (string) => ''

    Post status.

  • order (string) => ''

    Designates ascending ('ASC') or descending ('DESC') order.

  • title (string) => ''

    Section title.

array(

    /**
     * Number of posts to display.
     *
     * @type int
     */
    'max' => 0,

    /**
     * Post status.
     *
     * @type string
     * @default ''
     */
    'status' => '',

    /**
     * Designates ascending ('ASC') or descending ('DESC') order.
     *
     * @type string
     * @default ''
     */
    'order' => '',

    /**
     * Section title.
     *
     * @type string
     * @default ''
     */
    'title' => ''
);        


Usage

  1. if ( !function_exists( 'wp_dashboard_recent_posts' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/dashboard.php'; 
  3.  
  4. // An array of query and display arguments. 
  5. $args = array( 
  6. 'max' => 0, 
  7. 'status' => '', 
  8. 'order' => '', 
  9. 'title' => '' 
  10. ); 
  11.  
  12. // NOTICE! Understand what this does before running. 
  13. $result = wp_dashboard_recent_posts($args); 
  14.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/dashboard.php  
  1. function wp_dashboard_recent_posts( $args ) { 
  2. $query_args = array( 
  3. 'post_type' => 'post',  
  4. 'post_status' => $args['status'],  
  5. 'orderby' => 'date',  
  6. 'order' => $args['order'],  
  7. 'posts_per_page' => intval( $args['max'] ),  
  8. 'no_found_rows' => true,  
  9. 'cache_results' => false,  
  10. 'perm' => ( 'future' === $args['status'] ) ? 'editable' : 'readable',  
  11. ); 
  12.  
  13. /** 
  14. * Filters the query arguments used for the Recent Posts widget. 
  15. * @since 4.2.0 
  16. * @param array $query_args The arguments passed to WP_Query to produce the list of posts. 
  17. */ 
  18. $query_args = apply_filters( 'dashboard_recent_posts_query_args', $query_args ); 
  19. $posts = new WP_Query( $query_args ); 
  20.  
  21. if ( $posts->have_posts() ) { 
  22.  
  23. echo '<div id="' . $args['id'] . '" class="activity-block">'; 
  24.  
  25. echo '<h3>' . $args['title'] . '</h3>'; 
  26.  
  27. echo '<ul>'; 
  28.  
  29. $today = date( 'Y-m-d', current_time( 'timestamp' ) ); 
  30. $tomorrow = date( 'Y-m-d', strtotime( '+1 day', current_time( 'timestamp' ) ) ); 
  31.  
  32. while ( $posts->have_posts() ) { 
  33. $posts->the_post(); 
  34.  
  35. $time = get_the_time( 'U' ); 
  36. if ( date( 'Y-m-d', $time ) == $today ) { 
  37. $relative = __( 'Today' ); 
  38. } elseif ( date( 'Y-m-d', $time ) == $tomorrow ) { 
  39. $relative = __( 'Tomorrow' ); 
  40. } elseif ( date( 'Y', $time ) !== date( 'Y', current_time( 'timestamp' ) ) ) { 
  41. /** translators: date and time format for recent posts on the dashboard, from a different calendar year, see https://secure.php.net/date */ 
  42. $relative = date_i18n( __( 'M jS Y' ), $time ); 
  43. } else { 
  44. /** translators: date and time format for recent posts on the dashboard, see https://secure.php.net/date */ 
  45. $relative = date_i18n( __( 'M jS' ), $time ); 
  46.  
  47. // Use the post edit link for those who can edit, the permalink otherwise. 
  48. $recent_post_link = current_user_can( 'edit_post', get_the_ID() ) ? get_edit_post_link() : get_permalink(); 
  49.  
  50. $draft_or_post_title = _draft_or_post_title(); 
  51. printf( 
  52. '<li><span>%1$s</span> <a href="%2$s" aria-label="%3$s">%4$s</a></li>',  
  53. /** translators: 1: relative date, 2: time */ 
  54. sprintf( _x( '%1$s, %2$s', 'dashboard' ), $relative, get_the_time() ),  
  55. $recent_post_link,  
  56. /** translators: %s: post title */ 
  57. esc_attr( sprintf( __( 'Edit “%s”' ), $draft_or_post_title ) ),  
  58. $draft_or_post_title 
  59. ); 
  60.  
  61. echo '</ul>'; 
  62. echo '</div>'; 
  63.  
  64. } else { 
  65. return false; 
  66.  
  67.  
  68. return true;