This post was written both for myself and for anyone who's ever wondered — what's the difference between the “numberposts” and “posts_per_page” parameters passed to the get_posts function?
I sometimes find myself trying to recall the difference, and in various places I use one or the other.
But what’s the actual difference? For that, let’s take a look at the source code of the WordPress “get_posts” function (luckily, it’s not very large):
function get_posts( $args = null ) {
$defaults = array(
'numberposts' => 5,
'category' => 0, 'orderby' => '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);
}As you can see, the “numberposts” key is simply a wrapper for “posts_per_page”:
if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) ) $r['posts_per_page'] = $r['numberposts'];
Now let’s check the “WP_Query” object and look for any mention of the “numberposts” parameter.
Checked it? Took a look? Right — there are no mentions of it. But “posts_per_page” is used.
From everything stated above, we can conclude that “numberposts” was introduced just for convenience.
Whether you use “numberposts” or “posts_per_page” — is simply a matter of personal preference.
