Make WordPress Core

Changes between Version 1 and Version 13 of Ticket #58154


Ignore:
Timestamp:
08/28/2023 06:05:27 PM (3 months ago)
Author:
flixos90
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #58154

    • Property Keywords has-patch has-testing-info needs-testing added; needs-patch removed
    • Property Summary changed from Twenty Twenty-Three uses query block incorrectly to Block theme singular templates do not enter main query loop
    • Property Component changed from Bundled Theme to Editor
    • Property Milestone changed from 6.3 to 6.4
  • Ticket #58154 – Description

    v1 v13  
    1 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.
     1Most 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.
    22
    3 Concretely:
    4 * 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.
    5 * 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.
    6 * 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).
    7 * 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.
     3Related, 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.
    84
    9 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.
     5To test:
     61. Use Twenty Twenty-Three.
     72. In the Site Editor, update the `single` post template to not display the `core/post-featured-image` and `core/post-content` blocks.
     83. Add the below PHP code snippet to your WP installation.
     94. View a single post and notice the "in the loop: false" text.
     105. 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".
     11
     12{{{#!php
     13add_filter(
     14        'render_block_core/post-title',
     15        function( $block_content ) {
     16                return $block_content . '<p>in the loop: ' . ( in_the_loop() ? 'true' : 'false' ) . '</p>';
     17        }
     18);
     19}}}