#55722 closed enhancement (fixed)
Inconsistent guard conditions in query.php
Reported by: | vdankbaar | Owned by: | vdankbaar |
---|---|---|---|
Milestone: | 6.1 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Query | Keywords: | has-patch |
Focuses: | Cc: |
Description (last modified by )
While working on #55104 we noticed that other functions were also missing guard conditions.
In these cases, $wp_query could be null which would cause a fatal error. It would be safer to check if $wp_query is set before.
Change History (5)
This ticket was mentioned in PR #2714 on WordPress/wordpress-develop by vdankbaar.
2 years ago
#1
- Keywords has-patch added
SergeyBiryukov commented on PR #2714:
2 years ago
#3
Hi there, thanks for the PR!
For reference, here is the list of functions affected by this change:
have_posts()
in_the_loop()
rewind_posts()
the_post()
have_comments()
the_comment()
I had some notes when looking at the initial commit:
- These functions are not exactly conditional tags like the other
is_*()
functions:is_single()
,is_home()
, etc. So the same_doing_it_wrong()
messages referencing conditional tags are not quite accurate here. - Not all of these functions have a return value, e.g.
rewind_posts()
,the_post()
, orthe_comment()
. Introducing a return value seems a bit out of scope for this change and could use a separate discussion.
If the goal is to avoid a fatal error if $wp_query
is not set, I would like to suggest doing just that and returning early. That might not necessarily be consistent with the other is_*()
functions, but would be in line with the current return values of these particular functions. A _doing_it_wrong()
message could then be considered later as a separate enhancement.
With that in mind, I have made some adjustments to the PR and added a unit test. Any thoughts are welcome 🙂
Fixed inconsistent guard conditions in query.php in line with; #55104
Trac ticket: https://core.trac.wordpress.org/ticket/55722