[wp-trac] [WordPress Trac] #53824: Fatal error when in action pre_get_posts call *_template()

WordPress Trac noreply at wordpress.org
Thu Jul 29 14:10:48 UTC 2021


#53824: Fatal error when in action pre_get_posts call *_template()
--------------------------+---------------------
 Reporter:  Adiasz        |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  5.8.1
Component:  Query         |     Version:  5.8
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+---------------------
Changes (by SergeyBiryukov):

 * component:  General => Query
 * milestone:  Awaiting Review => 5.8.1


Comment:

 Hi there, welcome to WordPress Trac! Thanks for the report.

 I was able to reproduce the issue. It appears to be introduced in [51003]:

 * The code in question calls `get_page_template()` on the `pre_get_posts`
 action.
 * `get_page_template()` calls `get_query_template()`.
 * `get_query_template()` calls `locate_block_template()`.
 * `locate_block_template()` calls `resolve_block_template()`.
 * `resolve_block_template()` calls `get_block_templates()`.
 * `get_block_templates()` creates a new `WP_Query()` instance.
 * `WP_Query::get_posts()` runs the `pre_get_posts` action again, leading
 to endless recursion and a fatal error.

 Moving to 5.8.1 to see if we can figure out a solution.

 As a workaround, you should be able to check whether this is the main
 query before calling `get_page_template()`. This seems to work as expected
 in my testing:
 {{{
 add_action( 'pre_get_posts', function( $query ) {
         if ( $query->is_main_query() ) {
                 echo get_page_template();
         }
 });
 }}}

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


More information about the wp-trac mailing list