WordPress / WordPress/WordPress-Coding-Standards
Add sniff for calling WP_Query->get_posts()
Open
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 2.8k
- Forks
- 521
- Avg merge
- 5d 20h
- Merged PRs (30d)
- 1
Description
Is your feature request related to a problem?
Calling get_posts() directly from WP_Query may be mistaken as a getter for the posts property, however, it is not. Calling this outside of the constructor will add an additional query.
$args = [
'posts_per_page' => '10',
'post_type' => 'post',
'post_status' => 'publish',
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'cat' => 123
];
$my_query = new WP_Query( $args );
$my_posts = $my_query->get_posts();
Describe the solution you'd like
Call the posts property rather than the method.
$my_posts = $my_query->posts;
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the existing sniff implementations and tests for WordPress or WP_Query method calls. Use the issue's examples to define the target behavior: flag direct WP_Query->get_posts() calls and recommend the posts property, while confirming the behavior is covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100