WordPress / WordPress/WordPress-Coding-Standards

Add sniff for calling WP_Query->get_posts()

Open
#1,860 2 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.