get_posts( array $args = null )
Retrieves an array of the latest posts, or posts matching the given criteria.
Description
For more information on the accepted arguments, see the WP_Query documentation in the Developer Handbook.
The $ignore_sticky_posts
and $no_found_rows
arguments are ignored by this function and both are set to true
.
The defaults are as follows:
See also
Parameters
- $args
-
(Optional) Arguments to retrieve posts. See WP_Query::parse_query() for all available arguments.<br>
- 'numberposts'
(int) Total number of posts to retrieve. Is an alias of$posts_per_page
in WP_Query. Accepts -1 for all. Default 5.<br> - 'category'
(int|string) Category ID or comma-separated list of IDs (this or any children).<br> Is an alias of$cat
in WP_Query. Default 0.<br> - 'include'
(int[]) An array of post IDs to retrieve, sticky posts will be included.<br> Is an alias of$post__in
in WP_Query. Default empty array.<br> - 'exclude'
(int[]) An array of post IDs not to retrieve. Default empty array.<br> - 'suppress_filters'
(bool) Whether to suppress filters. Default true.<br>
Default value: null
- 'numberposts'
Return
(WP_Post[]|int[]) Array of post objects or post IDs.
Source
File: wp-includes/post.php
function get_posts( $args = null ) {
$defaults = array(
'numberposts' => 5,
'category' => 0, 'orderby' => 'date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'post',
'suppress_filters' => true
);
$r = wp_parse_args( $args, $defaults );
if ( empty( $r['post_status'] ) )
$r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
$r['posts_per_page'] = $r['numberposts'];
if ( ! empty($r['category']) )
$r['cat'] = $r['category'];
if ( ! empty($r['include']) ) {
$incposts = wp_parse_id_list( $r['include'] );
$r['posts_per_page'] = count($incposts); // only the number of posts included
$r['post__in'] = $incposts;
} elseif ( ! empty($r['exclude']) )
$r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
$r['ignore_sticky_posts'] = true;
$r['no_found_rows'] = true;
$get_posts = new WP_Query;
return $get_posts->query($r);
}
Changelog
Version | Description |
---|---|
1.2.0 | Introduced. |