WordPress.org

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp get recent posts

Description

Retrieve the most recent posts.

Usage

<?php wp_get_recent_posts$args$output ?>

Default Usage

<?php
$args = 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
);

$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>

For a full explanation of the arguments, see get_posts() and the WP_Query object.

Return Value

$results (array) 
List of post arrays (default) or objects depending on $output.
FALSE (boolean) 
There are no results and $output is OBJECT.

Parameters

$args
(array) (optional)
Default: array
$output
(string) (optional) Constant OBJECT, ARRAY_A
Default: ARRAY_A

Examples

This is an example that shows how to use the wp_get_recent_posts() function to list the recent 10 posts.

<h2>Recent Posts</h2>
<ul>
<?php
	$recent_posts = wp_get_recent_posts();
	foreach( $recent_posts as $recent ){
		echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </li> ';
	}
	wp_reset_query();
?>
</ul>

If you want to delimit more or less recent posts you have to put the number in the function parameter like this example below:

<h2>Recent Posts</h2>
<ul>
<?php
	$args = array( 'numberposts' => '5' );
	$recent_posts = wp_get_recent_posts( $args );
	foreach( $recent_posts as $recent ){
		echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </li> ';
	}
	wp_reset_query();
?>
</ul>

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:

<h2>Recent Posts</h2>
<ul>
<?php
	$args = array( 'numberposts' => '5', 'tax_query' => array(
			array(
				'taxonomy' => 'post_format',
				'field' => 'slug',
				'terms' => 'post-format-aside',
				'operator' => 'NOT IN'
			), 
			array(
				'taxonomy' => 'post_format',
				'field' => 'slug',
				'terms' => 'post-format-image',
 				'operator' => 'NOT IN'
			)
	) );
	$recent_posts = wp_get_recent_posts( $args );
	foreach( $recent_posts as $recent ){
		echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .   ( __($recent["post_title"])).'</a> </li> ';
	}
	wp_reset_query();
?>
</ul>

In the last example you can see the post_title wrapped in a __() function reference, this is used for internationalization (it's important in case you're dealing with multilanguage site).

Notes

  • Uses: wp_parse_args()
  • Uses: get_posts()
  • Only the value of ARRAY_A is checked for $output. Any other value or constant passed will return an array of objects.
  • This function returns posts in an associative array (ARRAY_A) format which is compatible with WordPress versions below 3.1. To get output similar to get_posts(), use OBJECT as the second parameter: wp_get_recent_posts( $args, OBJECT );

Change Log

  • Since: 1.0.0
  • 3.1.0: The $num parameter deprecated in favor of $args.

Source File

wp_get_recent_posts() is located in wp-includes/post.php.

Related

See also index of Function Reference and index of Template Tags.