WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.51k stars 4.2k forks source link

FSE post-content block is using wrong "first" item in the query loop #42862

Open Trekky12 opened 2 years ago

Trekky12 commented 2 years ago

Description

The post-content block in a query loop on a page with the global query loop shows the wrong content.

Apparently the problem lies in the post-content.php file (https://github.com/WordPress/WordPress/blob/aeb3b7530af57db885dfa8f377a7c6aa22e309bd/wp-includes/blocks/post-content.php#L42) where

the_post() 

is called. When this call is removed everything is working as expected. It seems this workaround for "third-party plugins" doesn't handle all edge cases. In this case the latest post on the whole site is saved as the_post() instead of the latest post of the query.

Maybe this is related to the issue #40743

Step-by-step reproduction instructions

  1. Use a FSE template.
  2. Create a template for the frontpage with the e.g. following content
    <!-- wp:template-part {"slug":"header","tagName":"header","className":"site-header"} /-->
    test
    <!-- wp:group {"tagName":"main","layout":{"inherit":true}} -->
    <main class="wp-block-group">
    <!-- wp:query {"queryId":0,"query":{"perPage":10,"pages":0,"offset":0,"postType":"post","categoryIds":[7],"tagIds":[],"order":"desc","orderBy":"date","search":"","exclude":[],"sticky":"","inherit":false}} -->
    <div class="wp-block-query">
        <!-- wp:post-template -->
            <!-- wp:post-title {"isLink":true} /-->
            <!-- wp:post-content /-->
        <!-- /wp:post-template -->
    <!-- /wp:query -->
    </main>
    <!-- /wp:group -->
    <!-- wp:template-part {"slug":"footer","tagName":"footer","className":"site-footer"} /-->
  3. The content is not matching the correct post. The block "post-content" shows the content of another post. When using post-excerpt the correct excerpt is used.

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Humanify-nl commented 2 years ago

I'm not the technical expert here, but worked a lot with Gutenberg and I'm not sure if this is intended behavior and something Gutenberg can handle currently. Yet it is an interesting use-case.

This is a challenge from design & technical viewpoints:

I can see a use-case where you want to show a full text post, (titles, paragaph, lists, etc), like old times if we wanted to show a blog query and show the first post in full. But this is probably a hard use-case to roll out dynamically.

Another idea is to go with functionality to give 'post-excerpt' block the functionality of returning the full body text. But it will still need to parse blocks to know what is the full body text, and how does the editor know what to show and what not?

Trekky12 commented 2 years ago

Hey @Humanify-nl,

thank you for your input. Actually I didn't think of the problem with recursive Query Loop blocks, but yes this could be a problem.

So do you think the use case to show full post contents in a Query Loop shouldn't be supported or do you think it is not easy to solve?

Despite these challenges there is still the problem that the content of another post is shown and the content doesn't match the post.

Humanify-nl commented 2 years ago

So do you think the use case to show full post contents in a Query Loop shouldn't be supported or do you think it is not easy to solve?

I think it is not easy to solve, yet I don’t know how the core team is looking at this.

My opinion is that this remains an edge case, which for now needs to be solved with a custom solution. But who knows what the future brings.

Trekky12 commented 2 years ago

Thank you for your opinion. Do you have an idea how a custom solution can be done?

t-hamano commented 2 years ago

This issue is very similar to #40743. Since I was able to reproduce the issue in this comment, I will remove Needs Testing label.

ryelle commented 1 year ago

I was also able to reproduce this with a "latest posts" homepage, where I have two Query Loops on index.html. One inherits the query from the page and shows title + date, the second is a specific category, and shows date + content. In this case, the Post Content block in the second Query Loop is overridden by the first post from the main query.

If I add a Post Content to that first Query Loop, the second one works.

I think it has something to do with how the content is (not) generated in get_the_content, specifically here, the conditional is true so it uses the globals instead of generating it from the correct post.

Unfortunately I'm not sure what a good fix is — when I tried removing the conditional around the_post here, I got a different incorrect post content… so I'm just sharing my tracking journey.

I'm going to remove the "post excerpt" label, since excerpts are working correctly as noted in the description and in my testing. Also, adding the "bug" label.