wp_get_recent_posts( array $args = array(), string $output = ARRAY_A ): array|false

Retrieves a number of recent posts.

Description

See also

Parameters

$argsarrayoptional
Arguments to retrieve posts.

Default:array()

$outputstringoptional
The required return type. One of OBJECT or ARRAY_A, which correspond to a WP_Post object or an associative array, respectively.

Default:ARRAY_A

Return

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

More Information

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

Source

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

Changelog

VersionDescription
1.0.0Introduced.

User Contributed Notes

  1. Skip to note 7 content

    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_item ) : ?>
    		<li>
    			<a href="<?php echo get_permalink($post_item['ID']) ?>">
    				<?php echo get_the_post_thumbnail($post_item['ID'], 'full'); ?>
    				//Assuming that the slider support captions 
    				<p class="slider-caption-class"><?php echo $post_item['post_title'] ?></p>
    			</a>
    		</li>
    	<?php endforeach; ?>
    </ul>
  2. Skip to note 8 content

    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' );
  3. Skip to note 10 content

    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>
  4. Skip to note 11 content

    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>
  5. Skip to note 12 content

    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>

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