Skip to content

fix: set up the edited post when rendering block previews over REST - #143

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:fix/render-endpoint-post-id
Oct 22, 2026
Merged

dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:fix/render-endpoint-post-id

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

Block templates rendered through the editor REST endpoints do not see the edited post consistently. On the edit screen (preloaded render) get_the_ID(), get_the_title() and get_the_author() return the post. Through /gutenberg/block/render/all they return nothing, and through /gutenberg/block/render/{name} (after an attribute change) get_the_author() and $id are empty.

Cause: gutenberg_block_render() reads post_id (includes/classes/rest.php:371), but the editor sends postId (getPostParams in src/blocks/components/block/index.tsx), so setup_postdata() never runs. The global post was only set as a side effect of the permission callback. gutenberg_block_render_all() never set a post at all; it only copies each block's params into $_GET.

Fix: the single route reads postId (falling back to post_id), the same parameter its permission callback already checks. The batch route sets up each block's post, but only when the user can edit that post, because the route itself only checks edit_posts.

Measured in Playground with a template printing get_the_ID(), get_the_title() and get_the_author():

  • before: preload 6 / Probe post / admin, batch empty for all three, single 6 / Probe post / (empty)
  • after: all three 7 / Probe post / admin

Tests: tests/unit/rest/BlockRenderPostTest.php covers the single route, the batch route and a contributor rendering a post they cannot edit. The first two fail on main and pass with the fix; tests/unit/rest passes (22/22). post-meta.md no longer says get_the_ID() only works on the frontend, and the LLM context is regenerated.

Related: #141 changes how Block::render() picks the post id from block context. The two changes do not overlap; with both, editor REST renders get the same post from $_GET, the global post and the block context.

The single render route read post_id while the editor sends postId, so
setup_postdata() never ran. The batch route never set a post at all.
Both now set up the edited post, the batch route only for posts the
user can edit.
@dnnsjsk
dnnsjsk merged commit 7b0a3c6 into inline0:main Oct 22, 2026
@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