We can use wp_get_recent_posts() function to retrieve number of recent posts.
Syntax
wp_get_recent_posts($args, $output)
Both of the parameters are optional.
$args parameter is the arguments we use to retrieve the posts. We can pass it as an array(). $output is a string. We can also pass it as an object or an associative array.
Only the value of ARRAY_A is checked for $output. Any other value or constant passed will return an array of objects.
The function will return an array of recent posts where the type of each element is determined by the $output parameter. It will return an empty array on failure.
To get output similar to get_posts(), use OBJECT as the second parameter: wp_get_recent_posts( $args, OBJECT );
Examples
List the 10 most-recent posts
Recent Posts
%2$s',
esc_url( get_permalink( $recent['ID'] ) ),
apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
);
}
?>
Limit number of recent posts
Recent Posts
'5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
printf( '- %2$s
',
esc_url( get_permalink( $recent['ID'] ) ),
apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
);
}
?>
Exclude posts of a specific post format
Recent Posts
'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( '- %2$s
',
esc_url( get_permalink( $recent['ID'] ) ),
apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
);
}
?>
Source: WordPress
code examples functions latest posts recent posts