Languages: English • get recent posts 日本語 Русский • (Add your language)
Retrieve the most recent posts.
<?php wp_get_recent_posts( $args, $output ) ?>
<?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.
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).
wp_get_recent_posts() is located in wp-includes/post.php
.