Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Block that displays post meta as nested block of Query Loop

You don’t want to use getCurrentPostId() from the core/editor store. This gets the current post ID from the post being edited, not the post in the query loop.

To get that, ensure your block accepts postId (and possibly postType) as block context values via the usesContext key in the block type metadata. This can be added in the block.json:

{
    "$schema": "https://schemas.wp.org/wp/6.6/block.json",
    "apiVersion": 3,
    "name": "vendor/block-name",
    …
    "usesContext": [
        "postId",
        "postType"
    ]
}

These values will then be available in the context prop that the edit component is given:

const Edit = ( { context } ) => {
    const blockProps = useBlockProps();

    const metaValue = useSelect( ( select ) => {
        const { getEntityRecord } = select( 'core' ); 
        
        if ( ! context.postId ) {
            return null;
        }

        // Get the meta for the post with the current post ID
        const post = getEntityRecord( 'postType', context.postType, context.postId );
        return post && post.meta ? post.meta['custom_link_text'] : null;
    }, [ context.postId, context.postType ] );

Presumably, this is a dynamic block with PHP-generated mark-up. If so, you may wish to consider using the same context values server-side for consistency:

<?php
// In `render.php` or similar.

$post_id = $block->context['postId'];

Related Posts:

  1. Saving a meta value as an object using useEntityProp
  2. Where can I find the Gutenberg block icons?
  3. Add pre-publish conditions to the block editor
  4. How to filter or remove default panels in Gutenberg PrePublish Panel
  5. Deactivate Gutenberg tips forever – not Gutenberg
  6. How does Gutenberg handle translations in React?
  7. Add To Gutenberg Sidebar
  8. How to detect the usage of Gutenberg
  9. Integrate Gutenberg as a Standalone App
  10. Help Unregistering a Core Block Type in Gutenberg
  11. How to edit a post meta data in a Gutenberg Block?
  12. How do I register multiple blocks with block.json and register_block_type_from_metadata?
  13. Remove block styles in the Block Editor
  14. Disable Gutenberg text-Settings in all blocks
  15. How do I get the current post ID within a Gutenberg/Block Editor block?
  16. Looking for all available options of Gutenberg Block
  17. Possible to use @wordpress/create-block with multiple blocks?
  18. Are there ways to make the Gutenberg editor wider? And the HTML-block higher?
  19. Blocks: set a default value for a TextControl
  20. gutenberg block – how to force update after attribute changed?
  21. What is the advantage of ‘register_block_type’ (the PHP function) when creating custom blocks?
  22. How to trigger JS in gutenberg page load
  23. Allow excerpt for pages in Gutenberg?
  24. WordPress Gutenberg Embed Blocks Are Not Responsive
  25. Is there a list / reference for all current native WordPress blocks?
  26. Gutenberg: Block validation failed
  27. Set fullscreen mode by default
  28. How to query multiple post types inside Gutenberg options panel?
  29. How can I add a custom attribute to Gutenberg core blocks?
  30. Gutenberg extend core blocks
  31. Setting default font family with theme.json
  32. How can I access core/paragraph textColor in a block template
  33. (Gutenberg Block Editor) Using editor.BlockEdit filter, need to alter html/ CSS class of BlockEdit Component
  34. Add Button To Top Toolbar in Gutenberg
  35. Saving post meta using the new EntityProvider APIs
  36. Disable device preview options in the block editor
  37. Add additional classes to gutenberg .editor-styles-wrapper
  38. Implementing Gutenberg RichText onSplit / onReplace
  39. Change order/position of Gutenberg inspector control panel
  40. Get the current block ID
  41. Gutenberg Inspector Controls
  42. How to use the WordPress < LinkControl /> Component
  43. What version of Gutenberg is included with WordPress?
  44. Implement Panel Color Inspector Control in Gutenberg
  45. Add Formatting Buttons to Gutenberg core/paragraph BlockControls
  46. How do you use __experimentalLayout to give innerblocks alignment control and default layout?
  47. WordPress Value of Undefined in Admin
  48. Remove border radius setting from the Gutenberg button block?
  49. Extending Gutenberg group block: How to properly combine multiple attributes?
  50. serialize_blocks breaking html tags in content
  51. All post types with getEntityRecords
  52. gutenberg dynamic block is returning 404
  53. Modify the core/paragraph block
  54. Gutenberg how to make attribute to save to meta
  55. Add PHP block template to content using wp_insert_post
  56. The Block Editor: Disable “Color settings” in the specific block
  57. Required (mandatory) Gutenberg block
  58. How to use getBlockIndex
  59. Gutenberg reusable blocks – is it possible to customise an instance?
  60. Gutenberg InspectorControls is deprecated, how to add custom block settings?
  61. Add classname to Gutenberg block wrapper in the editor?
  62. Hide or show Gutenberg custom block using date range
  63. How to disallow a certain custom gutenberg block outside of an InnerBlocks block?
  64. How should you internationalize javascript spread in multiple files but build in one?
  65. Use page Title in Gutenberg custom banner block
  66. How to add code to `head` with WordPress 5.9 FSE (Full Site Editing)
  67. Why is onChange={ ( content ) => setAttributes( { content } )} now used?
  68. Serialize custom block with InnerBlock
  69. How to develop custom blocks without triggering validation errors
  70. Dynamic gutenberg block with react instead of php
  71. get selected categories or tags (using javascript) in GutenBerg?
  72. File structure and react setup when creating multiple Gutenberg blocks
  73. Block Editor: add an aria-label to an option inside a SelectControl
  74. WordPress Gutenberg blocks: Input fields are not editable
  75. Gutenberg withInstanceId. When to use it?
  76. Gutenberg: Get All Attributes From «core/image» Block
  77. What replaces wpColorPicker in Gutenberg?
  78. How to make the Preview button automatically open new tab?
  79. Help with using getBlockIndex
  80. How to set column widths in a CPT block template?
  81. Gutenberg: How can I disable backspace key from deleting an empty paragraph block?
  82. Access GutenBerg data with Javascript?
  83. Gutenberg moving core blocks between categories
  84. Only show focused toolbar for Gutenberg Block with Multiple text fields
  85. Gutenberg: How to Change Post Status Programmatically?
  86. What’s the recommended Gutenberg component for adding a URL, in the toolbar/sidebar?
  87. Gutenberg block get categories in SelectControl
  88. Gutenberg Range Control default from meta value
  89. Gutenberg blocks – processing server data within a block
  90. How to access “default” property of attribute in Gutenberg
  91. Adding a custom PanelColorSettings control to a core block, and using the color slug in a custom className
  92. Once Again: How to Remove Option from Gutenberg Editor for Specific Block in theme.json
  93. Gutenberg Block manipulation: Undo parse_blocks() with serialize_blocks() results in unicode issues
  94. How to use “getEntityRecords” for user data?
  95. Where on Github, or elsewhere, can I find the code for core-blocks?
  96. Find all the places where a block type is used
  97. Enhancing Gutenberg featured image control
  98. Uncaught TypeError: wp.apiFetch is not a function
  99. Why is the new Gutenberg editor so narrow, and how to make it wider?
  100. WP Blocks – Gutenberg – not rendering $content
Categories block-editor Tags block-editor, post-meta
Link to a specifc category?
Footnotes are not copied as a block

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress