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

  • Creator
    Topic
  • #15533
    Resolved badabingbreda
    Participant

    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
    Replies
  • #15539
    Anh Tran
    Keymaster

    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?

    #15542
    badabingbreda
    Participant

    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.

    #15558
    Anh Tran
    Keymaster

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

    #15594
    badabingbreda
    Participant

    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. 🙂

    #15595
    Anh Tran
    Keymaster

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

    #15598
    badabingbreda
    Participant

    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.