Skip to content

Time to Read: Use block context postId to calculate reading time - #83022

Open
Malayt04 wants to merge 5 commits into
WordPress:trunkfrom
Malayt04:fix/post-time-to-read-query-loop
Open

Malayt04 wants to merge 5 commits into
WordPress:trunkfrom
Malayt04:fix/post-time-to-read-query-loop

Conversation

@Malayt04

Copy link
Copy Markdown

Fixes: #82971

What?

Updates core/post-time-to-read server-side rendering to fetch post content directly via get_post_field( 'post_content', $block->context['postId'] ) rather than relying on get_the_content().

Why?

When core/post-time-to-read is used inside a Query Loop on a Page, the server-side callback used get_the_content() which relies on the global $post and query state rather than the post provided by the block's context.

This caused the frontend to display an incorrect reading time (e.g. 1 minute instead of 8–10 minutes) and differed from the Block Editor (edit.js), which correctly reads the full post content directly from the post entity in context.

How?

  1. In packages/block-library/src/post-time-to-read/index.php, replaced get_the_content() with get_post_field( 'post_content', $block->context['postId'] ). This retrieves the full content directly for the specific post in context.
  2. Added a PHPUnit regression test (test_uses_block_context_post_id_not_global_post) in phpunit/blocks/render-post-time-to-read-test.php to verify that the block reads from context postId even when $GLOBALS['post'] is set to a different post.
  3. Added a bug fix entry in packages/block-library/CHANGELOG.md under ## Unreleased.

Testing Instructions

  1. Create a Post with a long body of text (~1,500+ words).
  2. Create a Page (e.g. /blog-page/) and insert a Query Loop block.
  3. Inside the Query Loop's Post Template, add the Time to Read block (core/post-time-to-read).
  4. In the editor, check the reading time (e.g., 8 minutes).
  5. View the Page on the frontend.
  6. Expected result: The frontend Time to Read block matches the editor (8 minutes), instead of showing an incorrect or truncated time (1 minute).

Testing Instructions for Keyboard

N/A (PHP server-side render fix only).

Screenshots or screencast

Before (Issue)

Before.mov

After (Fix)

After.mov

Use of AI Tools

AI assistance was used to draft the PHPUnit regression test using opencode.

Fetch post content directly via get_post_field() with the block context postId rather than relying on get_the_content(). This prevents reading time from being incorrectly truncated when rendered inside a Query Loop on a Page or when <!--more--> tags are present.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @Labyala.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: Labyala.

Co-authored-by: Malayt04 <malaytiwari@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions Bot added the [Package] Block library /packages/block-library label Sep 16, 2026
@github-actions

github-actions Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

👋 Welcome

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @Malayt04! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@Malayt04
Malayt04 marked this pull request as draft September 16, 2026 11:24
@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Sep 16, 2026
@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Block] Time to Read Affects the Time to Read Block labels Sep 16, 2026
@Malayt04
Malayt04 force-pushed the fix/post-time-to-read-query-loop branch from db1c560 to 3f2e221 Compare September 16, 2026 11:59
@Malayt04
Malayt04 marked this pull request as ready for review September 16, 2026 12:02
}

$content = get_the_content();
$content = get_post_field( 'post_content', $block->context['postId'] );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may have security implications, for example on a password protected post we would provide information about its lenght.
I'm not sure if the fix is right on packages/block-library/src/post-template/index.php we call $query->the_post() , so in teory get_the_content should work. Maybe we have an issue elsewhere. @Malayt04 would you be able to provide the markup you used for testing on your before video? Thank you in advance.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorgefilipecosta , thanks for the review, I should have shared the markup as well initially. The issue is not present normally, but when you put a More block (<!--more-->), this issue arises.

While $query->the_post() sets up the post in the loop, in a Query Loop (listing context) WordPress sets $more = 0 (teaser mode):

  • When a post contains a <!--more--> block, get_the_content() intentionally cuts off all content after that tag and returns only the teaser
  • Using get_post_field( 'post_content', $block->context['postId'] ) ensures we retrieve the complete article content

You are right about the password protected posts, we can add a post_password_required( $block->context['postId'] ) check so the block returns '' when a post is locked (something like this). Do let me know if this sounds reasonable, I will update the PR

Test Markup:

1. Post

<!-- wp:paragraph -->
<p>Short intro before the more tag...</p>
<!-- /wp:paragraph -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:paragraph -->
<p>1,500+ words of article body text here so that you can differentiate between time to read</p>
<!-- /wp:paragraph -->

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Time to Read Affects the Time to Read Block First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Block library /packages/block-library [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Post Time to Read: incorrect reading time in Query Loop on a Page (uses get_the_content() instead of context postId)

3 participants