Skip to content

fix: read the postId block context when rendering a block - #141

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:feat/block-context-post-id
Oct 22, 2026
Merged

dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:feat/block-context-post-id

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

A Blockstudio block rendered outside a main query gets $block['postId'] = 0, although WordPress hands it the post as postId block context. A block rendered through WP_Block with an explicit postId context and no global post gets an empty $post_id.

Measured with a probe block that prints $post_id, $block['postId'], $block['postType'] and $block['context']['postId'], on unchanged main:

Case $post_id $block['postId'] $block['postType'] context.postId
do_blocks() with a global $post (38), no queried object, as in a REST request or WP-CLI 38 0 post 38
( new WP_Block( $parsed, [ 'postId' => 39, 'postType' => 'post' ] ) )->render(), no global post '' 0 '' 39
Query Loop with two items (41, 42) on page 40 41 / 42 40 page 41 / 42
$_GET['postId'] = 43, global post 44 43 43 page 44

In the first row postId and postType disagree: get_post_type( 0 ) falls back to the global post. Tools that render outside a request currently have to set $_GET['postId'] to get a usable $block['postId'], which is what Blocks (preload) and Canvas_Page_Document do internally.

Cause

Block::render() (includes/classes/block.php, start of the method):

$post_id   = isset( $_GET['postId'] ) ? intval( $_GET['postId'] ) : get_the_ID();
$object_id = isset( $_GET['postId'] )
	? intval( $_GET['postId'] )
	: ( $is_editor ? $post_id : get_queried_object_id() );

$object_id becomes $block['postId']. The block type declares usesContext postId and postType (Build, where the WP_Block_Type is created), so $wp_block->context['postId'] is filled by render_block() from the global post and by core/post-template for each item, but render() never reads it. Without a queried object, get_queried_object_id() is 0.

Fix

  • $post_id: $_GET['postId'], then $wp_block->context['postId'] when $wp_block is a WP_Block with that context, then get_the_ID().
  • $block['postId']: $_GET['postId'], then the editor path as before, then get_queried_object_id(), and only when that is 0, $post_id.

$block['postId'] deliberately does not switch to the loop item inside a Query Loop: docs/docs/blocks/context.md documents it as the current post there, next to $block['context']['postId'] for the loop item, and templates may rely on that. With a queried object nothing changes. Without one (REST, WP-CLI, and frontend views without a queried object such as the posts index or search), $block['postId'] was 0 and is now the rendered post, which is also the post $block['postType'] already reported.

One sentence added to docs/docs/blocks/context.md, LLM context rebuilt with npm run build:llm.

Tests

tests/unit/block/BlockPostContextTest.php registers a probe block from a temporary directory (same pattern as BlockRepeaterMinRowsTest):

  • test_render_outside_the_main_query_uses_the_global_post: fails on main ($block['postId'] is '0', expected the post ID), passes with the fix.
  • test_block_context_post_reaches_the_template_without_a_global_post: fails on main ($post_id is ''), passes with the fix.
  • test_query_loop_items_keep_the_queried_post_in_block_post_id and test_request_post_id_wins_over_block_context: pass before and after, they pin the documented Query Loop behaviour and the $_GET['postId'] priority.

Run in WordPress Playground (WordPress 7.1.3, plugin and test theme mounted as in tests/wp-env), one PHPUnit run per test class: the new class is 2 failures on main and OK (4 tests, 22 assertions) with the fix. The other block classes and render, block-tags, islands, canvas, wpml and rest give identical results on main and on this branch (the failures there are the same tests on main, environment related; GithubUpdaterTest needs process isolation and cannot run in Playground). composer cs, PHPStan on includes/classes/block.php and npm run check are clean.

This touches the lines right below the render mode check that #140 moves into a helper; whichever lands second needs a trivial rebase, happy to do it.

Block::render() took the post from $_GET['postId'], get_the_ID() and
get_queried_object_id() only. A block rendered outside a main query, for
example through do_blocks() in a REST request or WP-CLI, got
$block['postId'] = 0 even though WordPress passed the post as postId
block context, and a block rendered through WP_Block with an explicit
postId context and no global post got an empty $post_id.

$post_id now uses the postId block context before get_the_ID(), and
$block['postId'] falls back to $post_id when there is no queried object.
An explicit $_GET['postId'] still wins, the editor path is unchanged, and
inside a Query Loop $block['postId'] stays the queried post as documented.
@dnnsjsk

dnnsjsk commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Included in Blockstudio 7.7.6, now officially released. This PR is merged into main as part of the reviewed release, with full CI and distribution checks passing for the release commit.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants