Skip to content

Posts, Post Types: Add unit tests for get_posts() filter suppression - #14015

Closed
irozum wants to merge 1 commit into
WordPress:trunkfrom
irozum:task/7326-get-posts-suppress-filters-tests
Closed

irozum wants to merge 1 commit into
WordPress:trunkfrom
irozum:task/7326-get-posts-suppress-filters-tests

Conversation

@irozum

@irozum irozum commented Oct 5, 2026

Copy link
Copy Markdown

This is a test-only PR adding PHPUnit coverage for get_posts()'s suppress_filters default, with no production code changes.

What's covered: get_posts() in src/wp-includes/post.php defaults suppress_filters to true, unlike WP_Query's own default of false. This was the fix for this ticket ([8795]) — before it, get_posts() queries were silently mutated by posts_where/posts_join-style filters that plugins register for the main query, which is what the original bug report described. That default is still present verbatim in trunk but had no direct test guarding it.

Tests added in tests/phpunit/tests/post/getPosts.php:

  • test_should_suppress_posts_where_filter_by_default() — registers a posts_where filter, calls get_posts() with no suppress_filters argument, and asserts the filter never fires.
  • test_suppress_filters_false_should_apply_posts_where_filter() — same filter, but calls get_posts( array( 'suppress_filters' => false ) ) and asserts the filter does fire, proving the flag is actually wired through to the underlying WP_Query rather than just documented.

Why these cases: together they pin down both sides of the behavior the ticket's fix introduced — the suppressed-by-default case (the actual bug fix) and the explicit opt-out (so a future regression that silently drops the override can't hide behind the default-only test passing). I verified each test is meaningful by temporarily flipping the suppress_filters default to false in src/wp-includes/post.php: the default-suppression test failed as expected, and reverting restored a green run. The whole Tests_Post_GetPosts class (60 tests) and --group 7326 pass, along with composer lint:errors and composer phpstan.

Tests are a follow-up to the changeset that fixed this ticket: Follow-up to [8795].

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Ticket selection/triage, writing the test cases, and verifying them (including the red/green check against a deliberately reverted fix). I reviewed the diff, ran the full test/lint/typecheck gate myself, and take responsibility for the change.

@github-actions

github-actions Bot commented Oct 5, 2026

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. You can also manually ask me to refresh this list by adding the props-bot label.

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

Props irozum.

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

@huzaifaalmesbah huzaifaalmesbah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, @irozum! I really appreciate you covering both sides of the suppress_filters default in get_posts(), the suppressed default and the explicit opt-out; the get_posts() tests didn't touch it before.

One note on the ticket: #7326 was closed as fixed in 2.6.2, so it can't take new commits. Coverage additions without a ticket of their own usually go under the release's test umbrella ticket, which for 7.2 is #65819. Would you be open to updating the Trac ticket: link in the description to that one?

I left one small note inline as well.

AI Review: Opus 5.5

Comment on lines +227 to +231
add_filter( 'posts_where', array( $this, 'filter_posts_where_set_flag' ) );
get_posts();
remove_filter( 'posts_where', array( $this, 'filter_posts_where_set_flag' ) );

$this->assertFalse( $this->posts_where_filter_fired );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MockAction can do the counting here, which would let the extra property and callback go, and the remove_filter() isn't needed since hooks are restored in tear_down().

Suggested change
add_filter( 'posts_where', array( $this, 'filter_posts_where_set_flag' ) );
get_posts();
remove_filter( 'posts_where', array( $this, 'filter_posts_where_set_flag' ) );
$this->assertFalse( $this->posts_where_filter_fired );
$filter = new MockAction();
add_filter( 'posts_where', array( $filter, 'filter' ) );
get_posts();
$this->assertSame( 0, $filter->get_call_count() );

The second test could do the same with assertSame( 1, ... ). What do you think?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified in the test-only regression coverage for get_posts() filter suppression.

No open issues 🎉

What changed in this PR

Adds regression coverage for get_posts() filter suppression, following up on the fix for ticket #7326. This is a test-only change with no production code modifications.

Changes:

  • Verifies that posts_where filters are suppressed by default.
  • Verifies that setting suppress_filters to false allows the filter to run.
File Description
tests/​phpunit/​tests/​post/​getPosts.php Adds two filter-suppression tests and a shared callback that records whether the filter ran.

Review effort: Balanced

@lancewillett lancewillett left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the changed assertions against existing coverage and production behavior. Randomized single-site and multisite runs passed 60 tests / 195 assertions with three seeds; PHPCS passes. Both targeted regression mutations fail as intended. No blockers. The suggested MockAction simplification and removal of redundant local hook cleanup are optional; they do not affect the regression coverage.

AI review · gpt-6

wporg-sync pushed a commit that referenced this pull request Oct 30, 2026
Verify that `get_posts()` suppresses `posts_where` filters by default and honors an explicit `suppress_filters` value of `false`.

Developed in: #14015

Props irozum, huzaifaalmesbah.
See #65819, #7326.


git-svn-id: https://develop.svn.wordpress.org/trunk@65101 602fd350-edb4-49c9-b593-d223f7449a82
@lancewillett

Copy link
Copy Markdown
Member

wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Oct 30, 2026
Verify that `get_posts()` suppresses `posts_where` filters by default and honors an explicit `suppress_filters` value of `false`.

Developed in: WordPress/wordpress-develop#14015

Props irozum, huzaifaalmesbah.
See #65819, #7326.

Built from https://develop.svn.wordpress.org/trunk@65101


git-svn-id: http://core.svn.wordpress.org/trunk@64251 1a063a9b-81f0-0310-95a4-ce76da25c4cd
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.

4 participants