wp_get_recent_posts( array $args = array(), string $output = ARRAY_A )
Retrieve a number of recent posts.
Contents
Description Description
See also See also
Parameters Parameters
- $args
-
(array) (Optional) Arguments to retrieve posts.
Default value: array()
- $output
-
(string) (Optional) The required return type. One of OBJECT or ARRAY_A, which correspond to a WP_Post object or an associative array, respectively.
Default value: ARRAY_A
Return Return
(array|false) Array of recent posts, where the type of each element is determined by $output parameter. Empty array on failure.
Source Source
File: wp-includes/post.php
function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { if ( is_numeric( $args ) ) { _deprecated_argument( __FUNCTION__, '3.1.0', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) ); $args = array( 'numberposts' => absint( $args ) ); } // Set default arguments. $defaults = array( 'numberposts' => 10, 'offset' => 0, 'category' => 0, 'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' => '', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private', 'suppress_filters' => true, ); $parsed_args = wp_parse_args( $args, $defaults ); $results = get_posts( $parsed_args ); // Backward compatibility. Prior to 3.1 expected posts to be returned in array. if ( ARRAY_A == $output ) { foreach ( $results as $key => $result ) { $results[ $key ] = get_object_vars( $result ); } return $results ? $results : array(); } return $results ? $results : false; }
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
1.0.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Limited recent posts thumbnails with captions
This example can be used to show a limited number of recent posts thumbnails in a slider with captions.
Sliders uses ids and/or classes on div tags and/or ul tags to apply the custom css and hook up the slideshow in js.
In this example, we will be using directly the ul tag.
Expand full source codeCollapse full source code
List the 10 most-recent posts
This is an example that shows how to use the wp_get_recent_posts() function to list the recent 10 posts.
Limit number of recent posts
If you want to delimit more or less recent posts you have to put the number in the function parameter like this example below:
Expand full source codeCollapse full source code
Exclude posts of a specific post format
To exclude posts with a certain post format, you can use
Class_Reference/WP_Query#Taxonomy_Parameters
like this next example, which excludes all posts with the ‘aside’ and ‘image’ formats:Expand full source codeCollapse full source code
Argument Filering Hook For Widgets
This function doesn’t have filters but in the default Recent Posts Widget there’s a hook that allows you filter the arguments.