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:

Description

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 https://github.com/WordPress/gutenberg/pull/49904, 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
#4

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

This is a mirror of https://github.com/WordPress/gutenberg/pull/49904, 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: https://core.trac.wordpress.org/ticket/59225

#5 @flixos90
9 months ago

  • Keywords has-testing-info needs-testing added

See https://github.com/WordPress/gutenberg/pull/49904 for detailed testing instructions.

@flixos90 commented on PR #5103:


9 months ago
#6

Note: The test failures here are related to https://core.trac.wordpress.org/ticket/58154 / #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 https://github.com/WordPress/gutenberg/pull/49904 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 https://wordpress.slack.com/archives/C055Y7FKS7N/p1694112328255489 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 https://github.com/WordPress/wordpress-develop/pull/5262 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.