Support

Account

Home Forums Gutenberg Get value of ACF field in Gutenberg block

Solving

Get value of ACF field in Gutenberg block

  • Hi Developers,

    I have set up an ACF-style Gutenberg block with a couple of select fields in it. The second field is populated conditionally on the output of the first, ie, the first field is a list of posts of post type “Resource” and the second field is a list of documents attached to the Resource posts. If I choose a resource, the list of documents attached to that resource is populated into the second select box.

    I’ve used the approach in this support thread to do it:

    https://support.advancedcustomfields.com/forums/topic/javascript-api-for-fields-in-block-edit-mode/

    The second field is populating fine now via an ajax call. The problem is, I want to set the “selected” value in the second dropdown to the current value of the ACF field. To do that I need to know what the current value is.

    I’ve tried using a lot of variations on

    get_field( $post_id, $selector)

    and

    acf_maybe_get_field( $selector, $post_id );

    which is what ACF is using to get the value. This doesn’t get me the value, I think because my plugin is outside the scope of the Gutenberg block environment… (ACF is doing some funky stuff with getting those values)

    Can anyone please help me to get the current value of the ACF field for the specific block? I don’t mind if it’s in PHP or in the JS API – I can use either.

    Thanks!

    • NickDA

    • September 20, 2019 at 4:06 am

    I also wanted to get meta data values in a block, and after trying everything, and eventually opening a ticket about it, and the simple answer I got was “it is not possible”.

    This should have been a hint, but I tried anyways: – https://www.advancedcustomfields.com/resources/blocks/

    Under the heading “Anywhere and Everywhere”, it says that “ACF blocks are not tied to metadata…”. I knew this going in, but I had to “force” a solution, needless to say, it was a total fail.

    • tamlyn

    • November 20, 2019 at 9:17 pm

    Does this help? https://support.advancedcustomfields.com/forums/topic/acf-5-8-parse-gutenberg-blocks-and-get-acf-data-outside-of-post/

    Essentially you load the post content, parse it for blocks then when you find an ACF block you can use get_field.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.