WordPress at Your Fingertips

wp_get_recent_posts() WP 1.0.0

Retrieve the last posts of the website, including drafts, scheduled, and posts on moderation. Gets the last 10 posts only.

In the post_type parameter, you can specify the posts of what type you want to get, by default, post - i.e., the basic posts of the site are retrieved...

The function is a wrapper for get_posts(). The difference is that it gets 10 instead of 5 records and tries to get posts of all possible statuses, including drafts, scheduled, personal, and pending moderation.

'numberposts' => 10,
'post_status' => 'draft, publish, future, pending, private',

No Hooks.



  • List of posts as an associative array of posts objects. The array indexes will contain post ID, and the value will contain the WP_Post object, i.e. the post itself.
  • Empty array - if there are no posts.
  • false - if $output != ARRAY_A and there are no posts.


<?php wp_get_recent_posts( $args, $output ); ?>

Usage Template

$result = wp_get_recent_posts( 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,
) );
foreach( $result as $post ){
	setup_postdata( $post );

	the_title(); // display

Arguments to retrieve posts. See the description of WP_Query() for which arguments you can use.

Note: to pass arguments became possible from WP version 3.1, before this the number was given to this parameter — how many last posts to output.
Default: empty array


The required return type.

  • ARRAY_A - array of arrays with posts data, array indexes will contain post IDs.
  • OBJECT - array of WP_Post objects, array indexes will contain numbers in order.

Default: ARRAY_A


#1 Last posts

Let's display a list of 6 links to the last already published posts from category 5:


$result = wp_get_recent_posts(array(
	'numberposts' => 6,
	'category' => 5,
	'post_status' => 'publish',

foreach( $result as $p ){ 
	— <a href="<?php echo get_permalink($p['ID']) ?>"><?php echo $p['post_title'] ?></a><br />    

#2 For WP versions below 3.1

This example shows how to use the wp_get_recent_posts() function to display a list of last 7 posts. The list will be in the form of links to posts.

	<h2>Posts Recentes</h2>
	$recent_posts = wp_get_recent_posts( 7 );
	foreach( $recent_posts as $post ){
		echo '<li><a href="' . get_permalink($post["ID"]) . '" title="'. esc_attr($post["post_title"]) .'" >'. esc_html($post["post_title"]) .'</a> </li> ';

The Data returned for each post

Post ID.
The ID of the author of the post.
Post date in format: YYYY-MM-DD HH:MM:SS (taking into account time correction).
Post date in format: YYYY-MM-DD HH:MM:SS (GMT/UTC).
Post text.
Post title.
Short description of the post.
Post status, can be: publish|pending|draft|private|static|object|attachment|inherit|future|trash.
The ability to leave a comments for the post, can be: open|closed|registered_only.
The status of the pings/trackbacks, can be: open|closed.
Post password.
Post name (slug). The name which is usually used in URL.
URLs from post content, to which WordPress will send notifications while post updating.
URLs from post content, which alreade recieved notifications.
Post modification date, in format: YYYY-MM-DD HH:MM:SS (taking into account time correction).
Post modification date, in format: YYYY-MM-DD HH:MM:SS (GMT/UTC).
ID of the parent post (is different from zero for attachments).
The unique identifier of the post (for RSS), usually contains URL to the post. You can not use this field as a permanent link to a post.
Sequence number for menu building (used for pages - heirarchical posts).
Post type: post, page, attachment etc.

What type of attachments to receive. Can be used when the parameter post_type = attachment.

  • image/jpeg
  • image/png
  • image/gif
  • image - for any images
  • audio/mpeg
  • application/pdf
  • application/zip

You can specify multiple types in an array, instead of a single MIME type. Full list of MIME types see here.

The number of comments.



Since 1.0.0 Introduced.

Code of wp_get_recent_posts() WP 5.7.2

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;


From tag: query

More from category: Queries

vladlu 100vlad.lu
Editors: Kama 100
No comments
    Log In