WordPress / WordPress/WordPress-Coding-Standards

Create sniff to make sure that wp_reset_postdata() is called after sub-loop

Open
#34 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Component: Extra Focus: Code analysis Focus: WP interoperability Type: Enhancement
Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Description

When creating new WP_Query instances and looping over them, it is important that the global post variables are reset to be reflect $wp_the_query. A sniff can check for instantiation of WP_Query followed by use of have_posts() and the_post() and if after this loop there does not exist a wp_reset_postdata(), there should be a warning flagged.

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 reviewing how existing PHP_CodeSniffer sniffs detect WP_Query usage and post loops. Trace the sequence of WP_Query, have_posts(), and the_post(), then determine how a missing wp_reset_postdata() should be reported after the loop. Done means the sniff reliably flags the missing reset without flagging loops that call it.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.