WordPress / WordPress/WordPress-Coding-Standards
Create sniff to make sure that wp_reset_postdata() is called after sub-loop
Nobody has claimed this yet.
- 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
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 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