Post Meta

Last modified:

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.

index.php
index.twig

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

      
Copy

          {{ post_id }};
  {{ postId }};
  {{ block.postId }};
  {{ b.postId }};

      
Copy

Getting post meta

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

ACF

index.php
index.twig

          echo get_field('field_name', $post_id);

      
Copy

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

      
Copy

Metabox

index.php
index.twig

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

      
Copy

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

      
Copy

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.

block.json

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

      
Copy

🍪 This website uses cookies to ensure you get the best experience on our website.

Accept Decline