wp_get_recent_posts()
Retrieve the last posts of the website, including drafts, scheduled, and posts on moderation. Gets the last 10 posts only.
In the post_type
parameter, you can specify the posts of what type you want to get, by default, post
- i.e., the basic posts of the site are retrieved...
The function is a wrapper for get_posts(). The difference is that it gets 10 instead of 5 records and tries to get posts of all possible statuses, including drafts, scheduled, personal, and pending moderation.
'numberposts' => 10, 'post_status' => 'draft, publish, future, pending, private',
No Hooks.
Return
Array|false
.
- List of posts as an associative array of posts objects. The array indexes will contain post ID, and the value will contain the WP_Post object, i.e. the post itself.
- Empty array - if there are no posts.
- false - if
$output != ARRAY_A
and there are no posts.
Usage
<?php wp_get_recent_posts( $args, $output ); ?>
Usage Template
$result = wp_get_recent_posts( 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, ) ); foreach( $result as $post ){ setup_postdata( $post ); the_title(); // display } wp_reset_postdata();
- $args(array)
Arguments to retrieve posts. See the description of WP_Query() for which arguments you can use.
Note: to pass arguments became possible from WP version 3.1, before this the number was given to this parameter — how many last posts to output.
Default: empty array- $output(строка)
The required return type.
- ARRAY_A - array of arrays with posts data, array indexes will contain post IDs.
- OBJECT - array of WP_Post objects, array indexes will contain numbers in order.
Default: ARRAY_A
Examples
#1 Last posts
Let's display a list of 6 links to the last already published posts from category 5:
<?php $result = wp_get_recent_posts(array( 'numberposts' => 6, 'category' => 5, 'post_status' => 'publish', )); foreach( $result as $p ){ ?> — <a href="<?php echo get_permalink($p['ID']) ?>"><?php echo $p['post_title'] ?></a><br /> <?php } ?>
#2 For WP versions below 3.1
This example shows how to use the wp_get_recent_posts() function to display a list of last 7 posts. The list will be in the form of links to posts.
<ul> <li> <h2>Posts Recentes</h2> <ul> <?php $recent_posts = wp_get_recent_posts( 7 ); foreach( $recent_posts as $post ){ echo '<li><a href="' . get_permalink($post["ID"]) . '" title="'. esc_attr($post["post_title"]) .'" >'. esc_html($post["post_title"]) .'</a> </li> '; } ?> </ul> </li> </ul>
#3 The Data returned for each post
- ID(int)
- Post ID.
- post_author(int)
- The ID of the author of the post.
- post_date(string)
- Post date in format: YYYY-MM-DD HH:MM:SS (taking into account time correction).
- post_date_gmt(string)
- Post date in format: YYYY-MM-DD HH:MM:SS (GMT/UTC).
- post_content(string)
- Post text.
- post_title(string)
- Post title.
- post_excerpt(string)
- Short description of the post.
- post_status(string)
- Post status, can be: publish|pending|draft|private|static|object|attachment|inherit|future|trash.
- comment_status(string)
- The ability to leave a comments for the post, can be: open|closed|registered_only.
- ping_status(string)
- The status of the pings/trackbacks, can be: open|closed.
- post_password(string)
- Post password.
- post_name(string)
- Post name (slug). The name which is usually used in URL.
- to_ping(string)
- URLs from post content, to which WordPress will send notifications while post updating.
- pinged(string)
- URLs from post content, which alreade recieved notifications.
- post_modified(string)
- Post modification date, in format: YYYY-MM-DD HH:MM:SS (taking into account time correction).
- post_modified_gmt(string)
- Post modification date, in format: YYYY-MM-DD HH:MM:SS (GMT/UTC).
- post_parent(int)
- ID of the parent post (is different from zero for attachments).
- guid(string)
- The unique identifier of the post (for RSS), usually contains URL to the post. You can not use this field as a permanent link to a post.
- menu_order(int)
- Sequence number for menu building (used for pages - heirarchical posts).
- post_type(string)
- Post type: post, page, attachment etc.
- post_mime_type(string)
What type of attachments to receive. Can be used when the parameter post_type = attachment.
- image/jpeg
- image/png
- image/gif
- image - for any images
- audio/mpeg
- application/pdf
- application/zip
You can specify multiple types in an array, instead of a single MIME type. Full list of MIME types see here.
- comment_count(int)
- The number of comments.
Notes
- See: get_posts()
Changelog
Since 1.0.0 | Introduced. |