Make WordPress Core


Ignore:
Timestamp:
09/01/2023 05:30:02 PM (8 months ago)
Author:
flixos90
Message:

Editor: Ensure main query loop is entered for singular content in block themes.

Block themes currently lack the means to trigger the main query loop for singular content, since they cannot reasonably use the core/query and core/post-template blocks which are intended only for displaying a list of posts. So far, the missing main query loop on singular block templates has been worked around by enforcing the loop in certain core/post-* blocks, which however causes other bugs.

This changeset ensures that the main query loop is still started for singular block theme templates, by wrapping the entire template into the loop, which will by definition only have a single cycle as it only encompasses a single post. This is currently the most reliable solution, since even if there were blocks to properly trigger the main query loop on singular content, it would be unrealistic to expect all existing block themes to update their templates accordingly. It may be revisited in the future.

Props gziolo, youknowriad, joemcgill, costdev, mukesh27, flixos90.
Fixes #58154.
See #59225, #58027.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template.php

    r56209 r56507  
    211211 * @global string   $_wp_current_template_content
    212212 * @global WP_Embed $wp_embed
     213 * @global WP_Query $wp_query
    213214 *
    214215 * @return string Block template markup.
    215216 */
    216217function get_the_block_template_html() {
    217     global $_wp_current_template_content;
    218     global $wp_embed;
     218    global $_wp_current_template_content, $wp_embed, $wp_query;
    219219
    220220    if ( ! $_wp_current_template_content ) {
     
    229229    $content = shortcode_unautop( $content );
    230230    $content = do_shortcode( $content );
    231     $content = do_blocks( $content );
     231
     232    /*
     233     * Most block themes omit the `core/query` and `core/post-template` blocks in their singular content templates.
     234     * While this technically still works since singular content templates are always for only one post, it results in
     235     * the main query loop never being entered which causes bugs in core and the plugin ecosystem.
     236     *
     237     * The workaround below ensures that the loop is started even for those singular templates. The while loop will by
     238     * definition only go through a single iteration, i.e. `do_blocks()` is only called once. Additional safeguard
     239     * checks are included to ensure the main query loop has not been tampered with and really only encompasses a
     240     * single post.
     241     *
     242     * Even if the block template contained a `core/query` and `core/post-template` block referencing the main query
     243     * loop, it would not cause errors since it would use a cloned instance and go through the same loop of a single
     244     * post, within the actual main query loop.
     245     */
     246    if ( is_singular() && 1 === $wp_query->post_count && have_posts() ) {
     247        while ( have_posts() ) {
     248            the_post();
     249            $content = do_blocks( $content );
     250        }
     251    } else {
     252        $content = do_blocks( $content );
     253    }
     254
    232255    $content = wptexturize( $content );
    233256    $content = convert_smilies( $content );
Note: See TracChangeset for help on using the changeset viewer.