Ticket #9300 (reopened defect (bug))

Opened 3 years ago

Last modified 13 months ago

WP_Query with posts_per_page and sticky posts

Reported by: yukataninja Owned by: anonymous
Priority: low Milestone: Future Release
Component: Query Version: 3.0.4
Severity: minor Keywords: needs-patch
Cc: brocheafoin

Description

When using WP_Query with showposts=some_number and you have one post set as a sticky that is out of the range of some_number, the query returned will have some_number plus one posts returned. If the sticky post is within the range of some_number then only some_number of posts is returned by WP_Query.

Example Normal: have 3 posts in order: Post 1, Post 2, Post 3

my_WPQuery = new WP_Query("showposts=2");

This will return Post 1 and Post 2

Example Error: have 3 posts in order: Post 1, Post 2, Post 3 (<-- make sticky)

my_WPQuery = new WP_Query("showposts=2");

This will return 3 posts (Post 3, Post 1, Post 2).

Expected to return 2 posts (Post 3, Post 1).

Example OK: have 3 posts in order: Post 1, Post 2 (<-- make sticky), Post 3

my_WPQuery = new WP_Query("showposts=2");

This will return 2 posts (Post 2, Post 1).

Change History

  • Keywords needs-patch added; wp_query query showposts sticky removed
  • Version set to 2.7
  • Milestone changed from Unassigned to 2.8
  • Milestone changed from 2.8 to Future Release

Punting due to feature freeze. Reconsider with next release.

  • Status changed from new to closed
  • Resolution set to invalid
  • Milestone Future Release deleted

there is an argument that lets you disable stickies. see how it's done in the recent posts widget.

  • Status changed from closed to reopened
  • Version 2.7 deleted
  • Resolution invalid deleted

I've never commented on the Wordpress Trac before, so forgive me if I'm doing this wrong. But I don't think this issue should be closed. Disabling stickies won't produce the intuitively desired result. From the original example:

have 3 posts in order: Post 1, Post 2, Post 3 (<-- make sticky)

my_WPQuery = new WP_Query("showposts=2");

This will return 3 posts (Post 3, Post 1, Post 2).

But intuitively it should return 2 posts (Post 3, Post 1). I've tried using the new "posts_per_page" and it seems to produce the same result.

  • Priority changed from normal to low
  • Component changed from General to Query
  • Severity changed from normal to minor
  • Milestone set to Future Release

You are completely correct in that it is not intuitive. But there's no easy workaround without re-engineering of the WP_Query class.

Intuitively, we'd need to look-up sticky posts first, and then change the query's LIMIT statement accordingly. Doing so, however, introduces a new issue, which is what happens when a sticky post should be returned as part of the result anyway. Quite arguably, we could end up stripping stickies from all non-singular queries where they might appear, but the performance impact is not benign.

The least problematic ends up being the current setup, with you using the caller_get_posts argument.

  • Cc brocheafoin added
  • Version set to 3.0.4
  • Summary changed from WP_Query with showposts and sticky posts to WP_Query with posts_per_page and sticky posts

Changing title so that it conforms to up-to-date parameter name (i.e. not showposts, which has been deprecated for a while).

I am still able to reproduce this bug in 3.0.4, so adding version info as well.

Once I find the time to install 3.1, I'll test for this bug and maybe take a stab at it.

Note: See TracTickets for help on using tickets.