Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
How to get current post id in Gutenberg custom block?

This is my block.js file. In 4th line, I have MediaUpload var, Under it I want current post id. How can I get it?

( function( blocks, editor, i18n, element, components, _ ) {
var el = element.createElement;
var RichText = editor.RichText;
var MediaUpload = editor.MediaUpload;

blocks.registerBlockType( 'gutenberg-examples/example-05-recipe-card', {
    title: i18n.__( 'Author card', 'gutenberg-examples' ),
    icon: 'index-card',
    category: 'layout',
    attributes: {
        writer: {
            type: 'array',
            source: 'children',
            selector: 'h2',
        },
        photography: {
            type: 'array',
            source: 'children',
            selector: 'h2',
        },
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The current postId is accessed by the block context by adding usesContext when registering the block type, eg:

blocks.registerBlockType( 'gutenberg-examples/example-05-recipe-card', {
    title: i18n.__( 'Author card', 'gutenberg-examples' ),
    icon: 'index-card',
    category: 'layout',
    attributes: {...},
    usesContext: [ 'postId' ]
});

By including context in your edit({attributes, setAttributes, context}) function, you will have access to the current postId with:

edit({attributes, setAttributes, context}){    
    const {postId} = context;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!