22525

showposts VS posts_per_page. showposts works perfect but deprecated!

Question:

I'm unable to substitute posts_per_page with showposts in order to limit the number of posts returned in a list. When I use showposts, the resulting menu list is correctly displayed according to the number of posts I specify in the showposts limiter. However, when I use posts_per_page, the post limiter number appears to be irrelevant. The resulting list shows all posts, exceeding the limiter count.

Examples:

This works perfectly:

$myrecentposts = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$catHidden",'showposts' => $cb2_recent_count)); foreach($myrecentposts as $idxrecent=>$post) {

However, when I sub in posts_per_page, this DOES NOT work...

$myrecentposts = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$catHidden",'posts_per_page' => $cb2_recent_count)); foreach($myrecentposts as $idxrecent=>$post) {

*I'm only trying to get posts_per_page to work because I understand that showposts has been deprecated.

Answer1:

showposts <strong>is</strong> deprecated. However, posts_per_page is for use with query_posts(), or more specifically, WP_Query::query().

numberposts is the equivalent argument for get_posts().

NOTE: I removed my original answer concerning incorrect handling of arguments inside get_posts().

posts_per_page is not a valid argument for get_posts() for semantic reasons, since it suggests the idea of <strong>pagination</strong>, something which get_posts() does not support.

For clarity, and on behalf of @RichardM's comment, here's the skinny I originally wrote;

<hr />

It's down to how get_posts() parses the arguments before passing them on to WP_Query.

I've cut it down to the real basics here;

function get_posts($args = null) { $defaults = array('numberposts' => 5); $r = wp_parse_args($args, $defaults); if (!empty($r['numberposts'])) $r['posts_per_page'] = $r['numberposts']; $get_posts = new WP_Query; return $get_posts->query($r); }

See how numberposts overwrites posts_per_page, not accounting the condition that posts_per_page is being passed?

Answer2:

There is a bug : <a href="http://core.trac.wordpress.org/ticket/15150" rel="nofollow">http://core.trac.wordpress.org/ticket/15150</a> it is fixed in 3.1

Recommend

  • Wordpress loop problem: Multiple loops, index.php and is_paged causing duplicate posts on next page
  • Woocommerce, How to edit the “added to cart” message
  • Why echoing a local variable inside the same function is not working? [duplicate]
  • Custom “reply to” email header in Woocommerce New Order email notification
  • Why is the compiler showing error “expected expression before stud”?
  • show div after process register, woocommerce
  • SSH in Bash Script Messing Up File Read
  • Yii2: Using Kartik Depdrop Widget?
  • How to determine the CCSID used in CPYFRMIMPF command?
  • php is_file always return false
  • Dynamically accessing properties of knockoutjs observable array
  • HTML download movie download link
  • AES padding and writing the ciphertext to a disk file
  • How to add date and time under each post in guestbook in google app engine
  • Knitr HTML Loop - Some HTML output, some R output
  • Change an a tag attribute in JavaScript based on screen width
  • How to show dropdown in excel using jrxml (jasper api)?
  • Convert array of 8 bytes to signed long in C++
  • Importing jscolor library in angular 2
  • Release, debug version and Authorization Google?
  • jquery mobile loadPage not working
  • Apache 2.4 - remove | delete | uninstall
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Free memory of cv::Mat loaded using FileStorage API
  • Trying to get generic when generic is not available
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • How to get Windows thread pool to call class member function?
  • Bitwise OR returns boolean when one of operands is nil
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?