Skip to content

Don't flag get_posts() when suppress_filters is set to false - #900

Open
tomjn wants to merge 4 commits into
Automattic:developfrom
tomjn:fix/899-get-posts-suppress-filters
Open

tomjn wants to merge 4 commits into
Automattic:developfrom
tomjn:fix/899-get-posts-suppress-filters

Conversation

@tomjn

@tomjn tomjn commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

get_posts(), wp_get_recent_posts() and get_children() got the "uncached unless suppress_filters is false" warning on every call, including calls that already pass 'suppress_filters' => false. The sniff never read the arguments. It now skips the warning for get_posts() and wp_get_recent_posts() when the args array sets the key to false or 0.

get_children() still warns, because both rulesets use its error code for the no-LIMIT query it runs by default.

It still warns when it cannot tell what the value is: args passed as a variable, a non-literal value, a query string, an array unpacked after the key, or an array that is only part of the argument.

In #899 I guessed the phpcs:ignore comment was breaking the array parsing. It was not. The same call warns without the comment.

Fixes #899

The sniff warned on every call to get_posts(), wp_get_recent_posts() and get_children() without reading the arguments, so code that already followed the advice in the message still got the warning.

Calls where the args are a variable or the value is not a literal false still warn, as the sniff cannot know what they hold.

Fixes Automattic#899.
@tomjn
tomjn requested a review from a team as a code owner October 1, 2026 12:47

@GaryJones GaryJones left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for picking this up, Tom, and for following #899 all the way through to a fix. It's good to finally have the sniff read the arguments rather than nagging on every call, and the tests for the awkward cases (duplicate keys, unpacking, nested keys) are a nice touch.

I've requested changes for one thing really: the exemption currently covers get_children() too, which quietly removes the no-LIMIT error that the VIPMinimum ruleset hangs off that error code (details inline). The warning message could do with a small tweak now as well.

Everything else is optional, so feel free to pick and choose or leave them for a follow-up. Thanks again for contributing!

Comment thread WordPressVIPMinimum/Sniffs/Functions/RestrictedFunctionsSniff.php Outdated
Comment thread WordPressVIPMinimum/Tests/Functions/RestrictedFunctionsUnitTest.inc Outdated
Comment thread WordPressVIPMinimum/Sniffs/Functions/RestrictedFunctionsSniff.php Outdated
Comment thread WordPressVIPMinimum/Sniffs/Functions/RestrictedFunctionsSniff.php Outdated
Comment thread WordPressVIPMinimum/Sniffs/Functions/RestrictedFunctionsSniff.php Outdated
Comment thread WordPressVIPMinimum/Sniffs/Functions/RestrictedFunctionsSniff.php Outdated
tomjn added 3 commits October 2, 2026 15:44
Both rulesets reuse the get_posts_get_children code for the no-LIMIT query that get_children() runs by default. Exempting it removed that error under WordPressVIPMinimum, and the warning under WordPress-VIP-Go.

The ruleset tests now cover the call, which shifts the expected line numbers after it.
The sniff now skips calls it can see set suppress_filters to false, so the warning only reaches calls where it could not tell. "This can be safely ignored" no longer fits those without saying when.
Three changes to how the sniff reads the args array:

- The array has to be the whole argument, so `[ 'suppress_filters' => false ] ?: $args` warns again.
- A lone `0` counts as well as `false`. WP_Query only checks that the value is falsy.
- Unpacking is found by its token, without building a string from the whole item.
@tomjn
tomjn requested a review from GaryJones October 2, 2026 15:11
@tomjn

tomjn commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

@GaryJones all addressed and ready for another review!

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.

Suppress filters in get_posts false positive

2 participants