[wp-trac] [WordPress Trac] #27282: WP_Query returns more results when there are sticky posts
WordPress Trac
noreply at wordpress.org
Thu Feb 12 17:28:58 UTC 2015
#27282: WP_Query returns more results when there are sticky posts
-------------------------------------------------+-------------------------
Reporter: markoheijnen | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Future
Component: Query | Release
Severity: normal | Version:
Keywords: stickies needs-unit-tests has-patch | Resolution:
| Focuses:
-------------------------------------------------+-------------------------
Changes (by bendoh):
* keywords: needs-patch stickies needs-unit-tests => stickies needs-unit-
tests has-patch
Comment:
There isn't a lot of traction on this ticket, but I would definitely call
this a bug. It doesn't seem like a lot of people care, I'll venture a
guess that in general, sticky posts have fallen out of favor. However this
is acutely affecting me, so I'll take a crack at it.
My read of the code in tells me that:
* (query.php:1738) The query given in the unit test will be considered
(illogically?) to be is_home, so sticky posts will be added to it.
* (query.php:3539-3577) Sticky posts will ever only show up if the query
is_home, paged == 1, ignore_sticky_posts false.
So when you ask for 3 posts, you might get more than 3 if there are sticky
posts.
The problem isn't exactly scalable either, if you have more than
$posts_per_page sticky posts, you'll always get a full page of just
stickies for paged=1, then paged > 1 won't have any of the sticky posts.
Maybe I'm overthinking it here, and I realize this is an old feature
that's falling out of favor anyway, so maybe it's entirely moot, and it
looks like this has been discussed to death in #23336.
I've attached a patch against the latest trunk which processes the sticky
posts earlier on in the process.
If ignore_sticky_posts is false, then the sticky posts are actually being
queried are selected using the same rules that were there before:
post_status='publish' and post_type=$post_type. Once those IDs are
resolved, the WHERE clause is updated to add the selected sticky posts
explictly, and the ORDER BY clause is prefixed to push the sticky posts to
the front.
This moves all of the work for aggregating the sticky posts into the
database query. An EXPLAIN on this query doesn't show anything too scary.
A bonus with this technique is that paging will work correctly with any
number of sticky posts. Will need some further testing, though.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/27282#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list