Make WordPress Core

Opened 9 months ago

Closed 8 months ago

#59225 closed defect (bug) (fixed)

Block themes do not trigger "the loop" correctly

Reported by: flixos90's profile flixos90 Owned by: flixos90's profile flixos90
Milestone: 6.4 Priority: high
Severity: normal Version:
Component: Editor Keywords: gutenberg-merge has-patch has-unit-tests has-testing-info
Focuses: Cc:


Currently, block themes do not handle "the loop" correctly. Since their existence, WordPress themes have been expected to have the main WP_Query object go through the loop of posts when rendering posts. This ensures the WordPress query loop is correctly handled, e.g. calling in_the_loop() will return true as expected.

In block themes, this doesn't happen correctly though, the loop is not started when rendering posts which breaks the long-standing expectation that is relied on in both WP core and the plugin ecosystem. This is because the core/post-template block, when configured to use the global query (via the inherit attribute), _clones_ the global query object rather than using it directly. This leads to the main query loop never being started since it is only started on the cloned instance.

The only reason this isn't entirely broken is a workaround in the core/post-content and core/post-featured-image blocks which forces the loop to start if it hasn't started yet. However, that itself is not a proper fix as it does not cover all core/post-* blocks (e.g. when rendering posts with only a title and date it wouldn't work), and more importantly introduces bugs on its own, such as #58027.

Change History (14)

#1 @flixos90
9 months ago

This ticket is fixed by Gutenberg PR, once that is merged and backported to WP core.

cc @hellofromTonya

#2 @devmuhib
9 months ago

Thanks for fixing the issue.

This ticket was mentioned in PR #5103 on WordPress/wordpress-develop by @flixos90.

9 months ago

  • Keywords has-patch has-unit-tests added; needs-patch removed

This is a mirror of, as this change technically needs to be made in Gutenberg first and then ported over to WP core. This PR is for reference purposes and also includes the relevant test coverage in a way that is adapted to be included in WP core.

Trac ticket:

#5 @flixos90
9 months ago

  • Keywords has-testing-info needs-testing added

See for detailed testing instructions.

@flixos90 commented on PR #5103:

9 months ago

Note: The test failures here are related to / #5104 and should be fixed once that PR has been committed to WordPress core. As such, that PR should be committed first, _before_ porting the Gutenberg changes from into core.

This ticket was mentioned in Slack in #core-editor by flixos90. View the logs.

9 months ago

This ticket was mentioned in Slack in #core by joemcgill. View the logs.

9 months ago

#9 @flixos90
9 months ago

In 56507:

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.

#10 @flixos90
9 months ago

  • Keywords needs-testing removed

Testing was already done in Gutenberg and here in combination with the work from #58154.

#11 @flixos90
8 months ago

@hellofromTonya For your visibility, I'm flagging this ticket here as one that would be great to get backported/synced to core sooner than later. I'm aware of the situation in which appears to be blocking at the moment, but once that is resolved it would be great to do a sync of the Gutenberg packages with some time left before Beta 1.

This ticket was mentioned in Slack in #core by marybaum. View the logs.

8 months ago

#13 @gziolo
8 months ago

It looks like the related changes from Gutenberg will land today with from #59411. It seems that there isn't more work necessary other than testing the integration afterward.

#14 @flixos90
8 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

Indeed, this is fixed as of [56710], specifically through the changes in:

  • src/wp-includes/blocks/post-content.php
  • src/wp-includes/blocks/post-featured-image.php
  • src/wp-includes/blocks/post-template.php
Note: See TracTickets for help on using tickets.