Skip to content

Query: Honor constraining query vars when injecting sticky posts - #16142

Open
MoS2mir wants to merge 2 commits into
WordPress:trunkfrom
MoS2mir:trac-63360
Open

MoS2mir wants to merge 2 commits into
WordPress:trunkfrom
MoS2mir:trac-63360

Conversation

@MoS2mir

@MoS2mir MoS2mir commented Nov 21, 2026

Copy link
Copy Markdown

What

Trac ticket: https://core.trac.wordpress.org/ticket/63360

When WP_Query injects sticky posts at the top of home-query results, the subquery that fetches stickies missing from the result set ignores the constraints of the current query. Because author__in, post_name__in, category__not_in, tag__not_in, tax_query, meta_query and similar arguments do not set the is_author/is_category/... conditionals, the query is treated as is_home, and all published sticky posts get prepended — including stickies by other authors or with other slugs than the ones explicitly requested.

Reproducer from the ticket (Query 3 and Query 4 of the wp-query-tester plugin):

new WP_Query( array( 'author__in' => array( 1 ) ) );          // prepends stickies by author 2
new WP_Query( array( 'post_name__in' => array( 'foo' ) ) );   // prepends every sticky post

Fix

Carry the constraining query vars from the current query into the sticky-fetch get_posts() call, so only stickies that match the explicit filters are injected. Stickies still come first, but explicit filters are now honored.

The carried vars are the constraint-style arguments that can be set while is_home remains true: author__in, author__not_in, post_name__in, category__in, category__not_in, category__and, tag__in, tag__not_in, tag__and, tag_slug__in, tag_slug__and, post_parent, post_parent__in, post_parent__not_in, tax_query, and meta_query. Scalar constraints that set their own conditional (author, cat, tag, name, date parts, s) never reach this code path, since the sticky block only runs for is_home queries.

post__in is deliberately not touched: its documented behavior is that sticky posts are prepended regardless.

Tests

Three new tests in tests/phpunit/tests/query/stickies.php:

  1. test_stickies_should_obey_author__in — stickies by other authors are not injected; a matching sticky is still moved to the top (fails on trunk).
  2. test_stickies_should_obey_post_name__in — a slug query returns only the requested post (fails on trunk).
  3. test_stickies_should_obey_category__not_in — stickies in an excluded term are not injected (fails on trunk).

Test results

  • New tests: 3/3 fail on trunk, 3/3 pass with the patch.
  • Tests_Query_Stickies: 10/10 pass, including the pre-existing sticky-order and post__not_in tests.
  • Query group: 1442 tests, 5890 assertions, OK.
  • Post group: 1236 tests, 3217 assertions, OK.
  • WP_Test_REST_Posts_Controller: 288 tests, 2642 assertions, OK (no change to the ignore_sticky behavior added in [59801]/[60197]).

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mosamirdev.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant