Context
When blocks are used inside a loop (for example, a Query block), the block is executed once for each iteration of the loop. Blockstudio provides handy shortcuts for accessing the current loop and outer context.
PHP
<div>
<h1>
This is the data of current
element inside the loop:
<?php echo $block['context']['postId'] ?>
<?php echo $block['context']['postType'] ?>
</h1>
<h1>
This is the data the current post:
<?php echo $block['postId'] ?>
<?php echo $block['postType'] ?>
</h1>
</div>
Twig
<div>
<h1>
This is the data of current
element inside the loop:
{{ block.context.postId }}
{{ block.context.postType }}
</h1>
<h1>
This is the data the current post:
{{ block.postId }}
{{ block.postType }}
</h1>
</div>
Available Variables
Inside your block templates, you have access to these variables:
| Variable | Shorthand | Description |
|---|---|---|
$attributes |
$a |
Block attributes |
$block |
$b |
Block data (name, title, postId, postType, context, etc.) |
$context |
$c |
Parent block context |
$innerBlocks |
– | InnerBlocks content |
Block Data
The $block / $b variable contains:
| Property | Description |
|---|---|
name |
Block name (e.g., my-theme/my-block) |
title |
Block title |
dir |
Block directory path |
url |
Block directory URL |
postId |
Current post ID |
postType |
Current post type |
context |
Loop context (when inside a Query block) |
Inside a Query block, postId stays the post being viewed, while $post_id and context.postId follow the current loop item. When there is no queried post, for example in a REST request or WP-CLI, postId is the post the block is rendered for, taken from the postId block context or the global post.
Parent Context
When using usesContext, access parent block data via $context:
<?php
$parent = $context['parent-block/name'];
echo $parent['someAttribute'];
?>
Blockstudio handles providesContext automatically. You only need to add usesContext to the child block. There is no need to manually declare providesContext in the parent block’s block.json, as Blockstudio registers it for all attributes.
See InnerBlocks Context for more details.
Block Context and Communication
Practical patterns: card grids, pricing tables, and section wrappers where blocks need to share data.