get_posts

Retrieve list of latest posts or posts matching criteria.

Description

(array) get_posts( (null) $args = null ); 

The defaults are as follows:

Returns (array)

List of posts.

Parameters (1)

0. $args — Optional. (null) => null
Arguments to retrieve posts. See WP_Query::parse_query() for all available arguments.

Options

  • numberposts (int) => 5

    Total number of posts to retrieve. Is an alias of $posts_per_page in WP_Query. Accepts -1 for all.

  • category (int|string) => ''

    Category ID or comma-separated list of IDs (this or any children). Is an alias of $cat in WP_Query.

  • include (array) => array()

    An array of post IDs to retrieve, sticky posts will be included. Is an alias of $post__in in WP_Query.

  • exclude (array) => array()

    An array of post IDs not to retrieve.

array(

    /**
     * Total number of posts to retrieve. Is an alias of $posts_per_page in WP_Query. Accepts -1 for
     * all.
     *
     * @type int
     * @default 5
     */
    'numberposts' => 5,

    /**
     * Category ID or comma-separated list of IDs (this or any children). Is an alias of $cat in
     * WP_Query.
     *
     * @type int|string
     * @default ''
     */
    'category' => '',

    /**
     * An array of post IDs to retrieve, sticky posts will be included. Is an alias of $post__in in
     * WP_Query.
     *
     * @type array
     * @default array()
     */
    'include' => array(),

    /**
     * An array of post IDs not to retrieve.
     *
     * @type array
     * @default array()
     */
    'exclude' => array()
);        


Usage

  1. if ( !function_exists( 'get_posts' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all available arguments. 
  5. $args = array( 
  6. 'numberposts' => 5, 
  7. 'category' => '', 
  8. 'include' => array(), 
  9. 'exclude' => array() 
  10. ); 
  11.  
  12. // NOTICE! Understand what this does before running. 
  13. $result = get_posts($args); 
  14.  

Defined (1)

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

/wp-includes/post.php  
  1. function get_posts( $args = null ) { 
  2. $defaults = array( 
  3. 'numberposts' => 5,  
  4. 'category' => 0, 'orderby' => 'date',  
  5. 'order' => 'DESC', 'include' => array(),  
  6. 'exclude' => array(), 'meta_key' => '',  
  7. 'meta_value' =>'', 'post_type' => 'post',  
  8. 'suppress_filters' => true 
  9. ); 
  10.  
  11. $r = wp_parse_args( $args, $defaults ); 
  12. if ( empty( $r['post_status'] ) ) 
  13. $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish'; 
  14. if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) ) 
  15. $r['posts_per_page'] = $r['numberposts']; 
  16. if ( ! empty($r['category']) ) 
  17. $r['cat'] = $r['category']; 
  18. if ( ! empty($r['include']) ) { 
  19. $incposts = wp_parse_id_list( $r['include'] ); 
  20. $r['posts_per_page'] = count($incposts); // only the number of posts included 
  21. $r['post__in'] = $incposts; 
  22. } elseif ( ! empty($r['exclude']) ) 
  23. $r['post__not_in'] = wp_parse_id_list( $r['exclude'] ); 
  24.  
  25. $r['ignore_sticky_posts'] = true; 
  26. $r['no_found_rows'] = true; 
  27.  
  28. $get_posts = new WP_Query; 
  29. return $get_posts->query($r); 
  30.