Custom Fields
Support MB Blocks Getting post ID from inside the Meta Box Block render_callback

  • Creator
  • #15533
    Resolved badabingbreda

    Hi there, because there isn’t a “Meta Box Block” Forum yet, tagging this in General.

    I want to get info on the Active Post WHEN EDITING, so when editing the blocks.

    I’ve tried a few options that usually work for me, like get_the_ID(), filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT ) but they don’t seem to work, because this is an ajax call probably.

    Any possibility to let me get to the currently editing post ID?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Author
  • #15539
    Anh Tran

    Yes, that’s right. The plugin uses ajax to render the block, and the post ID isn’t passed to the block. get_the_ID() probably will work only when viewing the post on the front end.

    Just curious, why do you need the post ID?


    Using my implementation of your Meta Box Block I can enable users to query other parts of the posts data as well. A very simple example for instance is a custom block that will show the terms of a category on the post. But for that, I will need at least the post ID.

    Other things might be author of the post, publish-date, or pulling in other custom field (not block) field-data. That last one seems far fetched but why not if it can be done?

    As you said, it works when rendering front-end. But that experience is somewhat off when, while using Gutenberg, I can’t get to the post id and use that.

    Anh Tran

    Hi, I got it. I’ll add the post ID in the next version.


    Changed line 99 in MetaBox.php into:

    call_user_func( $this->render_callback, $attributes, $is_preview , $post_id );

    Adding the $post_id does the trick for me. 🙂

    Anh Tran

    Oops, I forgot to pass the param to the render callback function. Thanks for your help!


    LOL, was just about to edit my previous comment that I spotted the change in the docs, but not in the actual source-code.

    Thanks for the quick fix. It worked out marvelously!

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.


Subscribe to our newsletter to receive news and tutorials for Meta Box and WordPress.

grid gear info copy caret-up caret-down trash