Automattic / Automattic/VIP-Coding-Standards

Add check for queries made without post_status or post_type

Open
#186 0 comments 0 reactions 0 assignees View on GitHub
Type: Enhancement
Dominant language
PHP
Stars
261
Forks
44
Avg merge
19m
Merged PRs (30d)
1

Description

See https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/459.

----

A sniff can be added to flag `get_posts()` and `WP_Query` instances that are made without supplying post_status or post_type. See rationale:

> ## Not defining post_status Or post_type
>
> By default the post_status of a query is set to publish for anonymous users on the front end. It is not set in any WP_ADMIN context including Ajax queries. Queries on the front end for logged in users will also contain an OR statement for private posts created by the logged in user, even if that user is not part of the site. This will reduce the effectiveness of MySQL indexes, specifically the type_status_date index.
>
> The same is true for post_type, if you know that only a certain post_type will match the rest of the query (for example for a taxonomy, meta or just general query) adding the post_type as well as the post_status will help MySQL better utilize the indexes as it’s disposal.

https://vip.wordpress.com/documentation/code-review-what-we-look-for/#not-defining-post_status-or-post_type

Contributor guide

Open the contributing guide

Research direction

Start by reading the linked WordPress-Coding-Standards issue and the VIP code-review guidance, then inspect the repository's existing PHP_CodeSniffer sniffs. Trace how calls to get_posts() and WP_Query are analyzed. Done means the new check flags queries that omit post_status or post_type, with behavior consistent with the stated rationale.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.