[wp-trac] [WordPress Trac] #58154: Block theme singular templates do not enter main query loop (was: Twenty Twenty-Three uses query block incorrectly)

WordPress Trac noreply at wordpress.org
Mon Aug 28 18:05:27 UTC 2023


#58154: Block theme singular templates do not enter main query loop
-------------------------------------------------+-------------------------
 Reporter:  flixos90                             |       Owner:  flixos90
     Type:  defect (bug)                         |      Status:  assigned
 Priority:  normal                               |   Milestone:  6.4
Component:  Editor                               |     Version:  6.1
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch has-testing-info needs-    |     Focuses:
  testing                                        |
-------------------------------------------------+-------------------------
Changes (by flixos90):

 * keywords:  has-patch => has-patch has-testing-info needs-testing
 * component:  Bundled Theme => Editor


Old description:

> I recently noticed several problems related to how the WordPress query
> loop is handled in block themes, which partially comes down to the core
> implementation itself, but also to incorrect usage in e.g. the default
> theme Twenty Twenty-Three.
>
> Concretely:
> * TT3 is not using the `core/query` and `core/post-template` blocks in
> its `page.html` and `single.html` template. While that may intuitively
> seem like the right thing to do since these templates only display a
> single post, it is not: It has always been a best practice to always go
> through the query loop (i.e. `while( have_posts() ) { the_post(); ...
> }`), classic themes have always been doing that - check e.g. TT1's
> `page.php` and `single.php` for reference.
> * Not going through the query loop leads to several problems as the loop
> is never started, which apparently were so far were partially addressed
> by workarounds in Gutenberg, e.g. automatically starting the loop when in
> the `core/post-content` block, or similarly later in the `core/post-
> featured-image` block. These are workarounds though that lead to other
> bugs like #58027.
> * Additionally, wherever TT3 uses the `core/query` block, it includes
> parameters that customize the query, which should not happen as those
> templates should use the global main query, similar to how the equivalent
> classic themes would do it. In other words, preferably all usages of the
> `core/query` block should solely set `{"query":{"inherit": true}}` and no
> other "query"-specific parameters (layout parameters are okay of course).
> * However, it appears that the Site Editor itself has no awareness of the
> global query, so omitting the additional parameters leads to problems
> there. As long as that is the case, it makes sense to keep the additional
> parameters, but at a minimum it has to be ensured that every `core/query`
> usage includes `query.inherit=true`, so that the global query is relied
> on in the frontend.
>
> There are a few other related aspects that need to be fixed in core &
> Gutenberg directly, but as a starting point we need to use the
> `core/query` block correctly in TT3.

New description:

 Most block theme templates for singular content (`is_singular()`) are
 missing the `core/query` and `core/post-template` blocks which are needed
 to start the main query loop. This breaks the long-standing expectation,
 which is relied on by core and many plugins.

 Related, yet a different problem is #59225. As part of that ticket, the
 workaround found in the `core/post-content` and `core/post-featured-image`
 blocks is removed accordingly, however that is not a coherent fix to the
 problem anyway.

 To test:
 1. Use Twenty Twenty-Three.
 2. In the Site Editor, update the `single` post template to not display
 the `core/post-featured-image` and `core/post-content` blocks.
 3. Add the below PHP code snippet to your WP installation.
 4. View a single post and notice the "in the loop: false" text.
 5. After applying the PR https://github.com/WordPress/wordpress-
 develop/pull/5104 to your WP installation and refreshing the URL, you
 should see "in the loop: true".

 {{{#!php
 add_filter(
         'render_block_core/post-title',
         function( $block_content ) {
                 return $block_content . '<p>in the loop: ' . (
 in_the_loop() ? 'true' : 'false' ) . '</p>';
         }
 );
 }}}

--

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


More information about the wp-trac mailing list