wp_get_recent_posts

Retrieve a number of recent posts.

Description

(array|false) wp_get_recent_posts( (array) $args = array(), (string) $output = ); 

Returns (array|false)

Array of recent posts, where the type of each element is determined by $output parameter. Empty array on failure.

Parameters (2)

0. $args — Optional. (array) => array()
Arguments to retrieve posts. Default empty array.
1. $output — Optional. (string)
The required return type. One of OBJECT or ARRAY_A, which correspond to a WP_Post object or an associative array, respectively. Default ARRAY_A.

Usage

  1. if ( !function_exists( 'wp_get_recent_posts' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Optional. Arguments to retrieve posts. Default empty array. 
  5. $args = array(); 
  6.  
  7. // Optional. The required return type. One of OBJECT or ARRAY_A, which correspond to 
  8. // a WP_Post object or an associative array, respectively. Default ARRAY_A. 
  9. $output = ''; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = wp_get_recent_posts($args, $output); 
  13.  

Defined (1)

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

/wp-includes/post.php  
  1. function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { 
  2.  
  3. if ( is_numeric( $args ) ) { 
  4. _deprecated_argument( __FUNCTION__, '3.1.0', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) ); 
  5. $args = array( 'numberposts' => absint( $args ) ); 
  6.  
  7. // Set default arguments. 
  8. $defaults = array( 
  9. 'numberposts' => 10, 'offset' => 0,  
  10. 'category' => 0, 'orderby' => 'post_date',  
  11. 'order' => 'DESC', 'include' => '',  
  12. 'exclude' => '', 'meta_key' => '',  
  13. 'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private',  
  14. 'suppress_filters' => true 
  15. ); 
  16.  
  17. $r = wp_parse_args( $args, $defaults ); 
  18.  
  19. $results = get_posts( $r ); 
  20.  
  21. // Backward compatibility. Prior to 3.1 expected posts to be returned in array. 
  22. if ( ARRAY_A == $output ) { 
  23. foreach ( $results as $key => $result ) { 
  24. $results[$key] = get_object_vars( $result ); 
  25. return $results ? $results : array(); 
  26.  
  27. return $results ? $results : false; 
  28.