Opened 3 years ago
Closed 2 years ago
#55104 closed defect (bug) (fixed)
is_main_query() fatal error on null
Reported by: | nhadsall | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 6.1 | Priority: | normal |
Severity: | major | Version: | |
Component: | Query | Keywords: | good-first-bug has-patch 2nd-opinion |
Focuses: | Cc: |
Description
In some cases, get_the_title( $posd_id )
and then plugins hook into the_title
calling is_main_query()
. In these cases, $wp_query
is null. This causes a fatal error. It would be safer to check if $wp_query
is set before.
Change History (17)
#2
@
3 years ago
- Keywords needs-patch good-first-bug added; 2nd-opinion removed
- Milestone changed from Awaiting Review to Future Release
It makes sense to add a guard condition for $wp_query
to is_main_query()
. All the other is_*()
conditionals (is_single()
, is_home()
, etc) that inspect $wp_query
have this guard condition and trigger a call to _doing_it_wrong()
when $wp_query
isn't set.
This ticket was mentioned in PR #2711 on WordPress/wordpress-develop by vdankbaar.
3 years ago
#3
- Keywords has-patch added; needs-patch removed
Added guard condition on $wp_query to is_main_query
Trac ticket: https://core.trac.wordpress.org/ticket/55104
This ticket was mentioned in PR #2714 on WordPress/wordpress-develop by vdankbaar.
3 years ago
#4
Fixed inconsistent guard conditions in query.php in line with; https://core.trac.wordpress.org/ticket/55104
Trac ticket: https://core.trac.wordpress.org/ticket/55722
SergeyBiryukov commented on PR #2711:
3 years ago
#7
Thanks for the PR!
I have updated the $version
argument of the _doing_it_wrong()
call to 6.1.0, which indicates the next major WordPress version, and added a unit test. Looks ready to merge now 🙂
#8
@
3 years ago
- Owner set to SergeyBiryukov
- Resolution set to fixed
- Status changed from new to closed
In 53395:
SergeyBiryukov commented on PR #2711:
3 years ago
#9
#10
@
3 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
This failing hard with a fatal error seems like a benefit, and is something I originally anticipated. It's not something we could have introduced after the fact for many of the 2.x is_*
functions, but because is_main_query
was introduced much later (3.3), we could get away with doing so. You'd only get a fatal if you call this too early in WordPress load. Given this function has existed in this manner for ~11 years without incident, I'd consider reversion.
#11
@
3 years ago
- Keywords 2nd-opinion added
Thanks! I see the point, but the issue from the ticket description seems valid and a _doing_it_wrong()
message seems like a better developer experience than a fatal error here. Curious to see what others think.
#13
@
3 years ago
In [53396] I think test_conditional_tags_trigger_doing_it_wrong_and_return_false_if_wp_query_is_not_set
could benefit from a data provider rather than a foreach loop.
This failing hard with a fatal error seems like a benefit, and is something I originally anticipated.
@nacin Your thinking at the time is not documented on #18677, do you recall what it was? This is a somewhat optimistic question as it was 11 years ago.
SergeyBiryukov commented on PR #2714:
3 years ago
#15
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 🙂
Removing
trunk
as this was not introduced during the 6.0 cycle.Adding
2nd-opinion
to gather thoughts on this.