Home Forums Gutenberg Get value of ACF field in Gutenberg block


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:

    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)


    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.


    • 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: –

    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?

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

  • You can get acf custom field’s value from block’s data.

    function get_block_data($post, $block_name = 'core/heading', $field_name = "" ){
    	$content = "";
    	if ( has_blocks( $post->post_content ) && !empty($field_name )) {
    	    $blocks = parse_blocks( $post->post_content );
    	    foreach($blocks as $block){
    		    if ( $block['blockName'] === $block_name ) {
    		    	if(isset($block["attrs"]["data"][$field_name ])){
                       $content = $block["attrs"]["data"][$field_name ];
    	return $content;
Viewing 4 posts - 1 through 4 (of 4 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.