Post Meta

View as Markdown

Since block templates are just normal PHP files, you can use any PHP code you want in them. However, since blocks don’t have access to the global $post variable inside the editor, get_the_ID() and other functions that depend on the variable will only work on the frontend.

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"
    ]
  }
}