wp_get_recent_posts( array $args = array(), string $output = ARRAY_A )

Retrieve a number of recent posts.


Description Description


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


Top ↑

Return Return

(array|false) Array of recent posts, where the type of each element is determined by $output parameter. Empty array on failure.


Top ↑

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
	);

	$r = wp_parse_args( $args, $defaults );

	$results = get_posts( $r );

	// 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;

}

Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

    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.

    <h2>Recent Posts</h2>
    <ul>
    <?php
    	$recent_posts = wp_get_recent_posts();
    	foreach( $recent_posts as $recent ) {
    		printf( '<li><a href="%1$s">%2$s</a></li>',
    			esc_url( get_permalink( $recent['ID'] ) ),
    			apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
    		);
    	}
    ?>
    </ul>
    
  2. Skip to note content
    Contributed by Codex

    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:

    <h2>Recent Posts</h2>
    <ul>
    <?php
    	$args = array( 'numberposts' => '5' );
    	$recent_posts = wp_get_recent_posts( $args );
    	foreach( $recent_posts as $recent ){
    		printf( '<li><a href="%1$s">%2$s</a></li>',
    			 esc_url( get_permalink( $recent['ID'] ) ),
    			 apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
    		 );
    	}
    ?>
    </ul>
    
  3. Skip to note content
    Contributed by Codex

    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:

    <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 ){
    		printf( '<li><a href=%1$s">%2$s</a></li>',
    			esc_url( get_permalink( $recent['ID'] ) ),
    			apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
    		);
    	}
    ?>
    </ul>
    
  4. Skip to note content
    Contributed by LebCit

    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.

    <ul id="slider-id" class="slider-class">
    	<?php
    	$recent_posts = wp_get_recent_posts(array(
    		'numberposts' => 4, // Number of recent posts thumbnails to display
    		'post_status' => 'publish' // Show only the published posts
    	));
    	foreach($recent_posts as $post) : ?>
    		<li>
    			<a href="<?php echo get_permalink($post['ID']) ?>">
    				<?php echo get_the_post_thumbnail($post['ID'], 'full'); ?>
    				//Assuming that the slider support captions 
    				<p class="slider-caption-class"><?php echo $post['post_title'] ?></p>
    			</a>
    		</li>
    	<?php endforeach; wp_reset_query(); ?>
    </ul>
    
  5. Skip to note content
    Contributed by Howdy_McGee

    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.

    function filter_recent_posts_widget_parameters( $params ) {
       $params['orderby'] = 'date';
       
       return $params;
    }
    add_filter( 'widget_posts_args', 'filter_recent_posts_widget_parameters' );

You must log in before being able to contribute a note or feedback.