get_posts [ WordPress Function ]
| Parameters: |
|
| Uses: |
|
| Links: | |
| Returns: |
|
| Defined at: |
|
Retrieve list of latest posts or posts matching criteria.
The defaults are as follows: 'numberposts' - Default is 5. Total number of posts to retrieve. 'offset' - Default is 0. See {@link WP_Query::query()} for more. 'category' - What category to pull the posts from. 'orderby' - Default is 'post_date'. How to order the posts. 'order' - Default is 'DESC'. The order to retrieve the posts. 'include' - See {@link WP_Query::query()} for more. 'exclude' - See {@link WP_Query::query()} for more. 'meta_key' - See {@link WP_Query::query()} for more. 'meta_value' - See {@link WP_Query::query()} for more. 'post_type' - Default is 'post'. Can be 'page', or 'attachment' to name a few. 'post_parent' - The parent of the post or post type. 'post_status' - Default is 'publish'. Post status to retrieve.
Source
<?php
function get_posts($args = null) {
$defaults = array(
'numberposts' => 5, 'offset' => 0,
'category' => 0, 'orderby' => 'post_date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'post',
'suppress_filters' => true
);
$r = wp_parse_args( $args, $defaults );
if ( empty( $r['post_status'] ) )
$r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
$r['posts_per_page'] = $r['numberposts'];
if ( ! empty($r['category']) )
$r['cat'] = $r['category'];
if ( ! empty($r['include']) ) {
$incposts = wp_parse_id_list( $r['include'] );
$r['posts_per_page'] = count($incposts); // only the number of posts included
$r['post__in'] = $incposts;
} elseif ( ! empty($r['exclude']) )
$r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
$r['ignore_sticky_posts'] = true;
$r['no_found_rows'] = true;
$get_posts = new WP_Query;
return $get_posts->query($r);
}
?>
Examples [ wp-snippets.com ]
Top Google Search Results
- get_posts() - WordPress Codex
get_posts() makes use of the WP_Query class to fetch posts. See the parameters section of the WP_Query documentation for a list of parameters that this ...
codex.wordpress.org - WordPress › Support » Does get_posts() support tax_query?
The codex says that get_posts uses all the same arguments as query_posts, so why doesn't the tax_query work with get_posts? $myquery['tax_query'] = array( ...
wordpress.org - get_posts seems to be skipping the last Post - WordPress
Feb 23, 2012 ... I have custom post type (Events) with 6 events. $posts = get_posts( array(' post_type' => 'events') ); echo '<!-- '; print_r($posts); echo ' -->'; ...
wordpress.stackexchange.com - Custom Fields in get_posts
May 16, 2012 ... How do I query posts using get_posts and custompress custom fields? For example including this in my get_posts arguments... 'meta_query' =>
premium.wpmudev.org
User discussions [ wordpress.org ]
- dhtbrowne on "get_posts() orderby field isn't doing ANYTHING"
- monsurvey2 on "I need help in get_posts function with ajax file"
- nonverbla on "Global Posts Ordering - can't get it to work"
- anve on "Get direct successor of given ID"
- Fire Truck on "Get direct successor of given ID"
- anve on "Get direct successor of given ID"
- Digital Raindrops on "Issues displaying an array of posts"
- cw17s0n on "Issues displaying an array of posts"
- Digital Raindrops on "Issues displaying an array of posts"
- cw17s0n on "Issues displaying an array of posts"