Skip to content

Post Meta

Since block templates are just normal PHP files, you can use any PHP code you want in them. In the post editor, block previews are rendered with the edited post as the global $post variable, so get_the_ID() and other functions that depend on it work there too. Editors without an edited post, such as the Site Editor, have no global $post.

Blockstudio provides four different ways to access the current post ID in the editor and the frontend.

PHP

PHP
echo $post_id;
echo $postId;
echo $block['postId'];
echo $b['postId'];

Twig

Twig
{{ post_id }}
{{ postId }}
{{ block.postId }}
{{ b.postId }}

Getting Post Meta

All common custom field plugins and WordPress itself provide the possibility to access post meta using a post ID.

ACF

PHP

PHP
echo get_field('field_name', $post_id);

Twig

Twig
{{ fn('get_field', 'field_name', postId) }}

Metabox

PHP

PHP
echo rwmb_get_value('field_id', null, $post_id);

Twig

Twig
{{ fn('rwmb_get_value', 'field_id', postId) }}

Refreshing Blocks

By default, blocks won’t be refreshed when you save or update a post. Thus, old data might be displayed in the editor after saving. To fix this, you can use the refreshOn key inside your block.json to tell Blockstudio to refresh the block when a post is saved.

JSON
{
  "name": "blockstudio/native",
  "title": "Native Block",
  "category": "text",
  "icon": "star-filled",
  "description": "Native Blockstudio block.",
  "blockstudio": {
    "refreshOn": [
      "save"
    ]
  }
}