[wp-trac] [WordPress Trac] #63522: Warnings related to get_queried_object returning null

WordPress Trac noreply at wordpress.org
Wed Jun 4 03:07:55 UTC 2025


#63522: Warnings related to get_queried_object returning null
----------------------------------------+------------------------------
 Reporter:  dd32                        |       Owner:  (none)
     Type:  defect (bug)                |      Status:  new
 Priority:  low                         |   Milestone:  Awaiting Review
Component:  General                     |     Version:
 Severity:  minor                       |  Resolution:
 Keywords:  has-patch needs-unit-tests  |     Focuses:
----------------------------------------+------------------------------

Old description:

> Similar to #29660 and resolved for a lot of things in [54496],
> get_body_class() and wp_list_pages() throw many PHP warnings due to
> missing post objects when the request is singular, but no post is
> located.
>
> These include in `get_body_class()`:
> > E_WARNING: Attempt to read property "ID" on null in wp-includes/post-
> template.php:676
> > E_WARNING: Attempt to read property "post_type" on null in wp-includes
> /post-template.php:677
>
> and in `wp_list_pages()`:
> > E_WARNING: Attempt to read property "post_type" on null in wp-includes
> /post-template.php:1356
>
> and again in `feed_links_extra()`:
> > E_WARNING: Attempt to read property "comment_count" on null in wp-
> includes/general-template.php:3351
> > E_WARNING: Attempt to read property "ID" on null in wp-includes
> /general-template.php:3359
> (although in this case, it's not get_queried_object(), just an
> expectation of `get_post(0)` working, which is effectively the same
> thing)

New description:

 Similar to #29660 and resolved for a lot of things in [54496],
 get_body_class() and wp_list_pages() throw many PHP warnings due to
 missing post objects when the request is singular, but no post is located.

 These include in `get_body_class()`:
 > E_WARNING: Attempt to read property "ID" on null in wp-includes/post-
 template.php:676
 > E_WARNING: Attempt to read property "post_type" on null in wp-includes
 /post-template.php:677

 and in `wp_list_pages()`:
 > E_WARNING: Attempt to read property "post_type" on null in wp-includes
 /post-template.php:1356

 and again in `feed_links_extra()`:
 > E_WARNING: Attempt to read property "comment_count" on null in wp-
 includes/general-template.php:3351
 > E_WARNING: Attempt to read property "ID" on null in wp-includes/general-
 template.php:3359
 (although in this case, it's not get_queried_object(), just an expectation
 of `get_post(0)` working, which is effectively the same thing)

 and finally, in `redirect_canonical()`, when the permalink to a post can't
 be found:
 > Warning: Undefined array key "scheme" in wp-includes/canonical.php on
 line 752
 > Warning: Undefined array key "host" in wp-includes/canonical.php on line
 xxx

--

Comment (by dd32):

 Added an extra bonus in `redirect_canonical()`
 https://github.com/dd32/wordpress-
 develop/commit/648ad2a505e1817c9424800feacc06b0801fe630
 > redirect_canonical(): Don't attempt a ?page to /page/ redirect if the
 base url is unknown.
 > When redirect_canonical() attempts to redirect from ?page=2 to /page/2/
 if the base-url is unknown the result is an attempt to redirect to `/2/`
 which through a series of unfortunate assumptions in the code (Only full
 URLs are expected) results in an attempt to redirect to
 `://hostname.example/2/` and causes a series of php warnings such as:
 >
 > {{{
 > Warning: Undefined array key "scheme" in wp-includes/canonical.php on
 line 752
 > Warning: Undefined array key "host" in wp-includes/canonical.php on line
 717
 > Warning: Undefined array key "host" in wp-includes/canonical.php on line
 728
 > Warning: Undefined array key "host" in wp-includes/canonical.php on line
 731
 > }}}
 >
 > By only attempting to perform the singular pagination redirects when we
 actually have a singular permalink to redirect to, it prevents a chain of
 awkward warnings that appear unrelated.

 Unfortunately I couldn't immediately figure out how to trigger it on a
 vanilla install, but here's the type of request from GlotPress:
 {{{
 GET https://translate.wordpress.test/projects/meta/wordpress-org/en-
 au/default/?name=blahblahblah&page=2
 }}}
 which eventually attempts:
 {{{
 redirect_canonical( $requested_url =
 '://translate.wordpress.test/2/?name=blahblahblah', $do_redirect = FALSE )
 }}}

 As such, there's an alternate way that could be avoided, and may work
 better in some situations, but the above change resolves the main warning.
 {{{#!diff
 Index: wp-includes/canonical.php
 ===================================================================
 --- wp-includes/canonical.php   (revision 59611)
 +++ wp-includes/canonical.php   (working copy)
 @@ -615,6 +618,11 @@
                 unset( $redirect['port'] );
         }

 +       // If the redirect scheme is unknown, default to the current
 scheme.
 +       if ( empty( $redirect['scheme'] ) ) {
 +               $redirect['scheme'] = is_ssl() ? 'https' : 'http';
 +       }
 +
         // Trailing /index.php.
         $redirect['path'] = preg_replace( '|/' . preg_quote(
 $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] );
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/63522#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list