Support

Account

Home Forums Gutenberg Getting data from custom fields outside of blocks

Solved

Getting data from custom fields outside of blocks

  • I’ve been trying to affect a block, based on an external custom field but with no luck! So i wanted to check and see if anyone has managed to do this, if i am missing something daft or if it is simply not possible!

    What i mean is, i have a custom field that appears in the side bar of every page of type “Page”. It’s a select which returns a value between 1 and 4.

    Then, i have a custom block for the page hero area.

    What i would like to be able to do is, inside the hero block, check the value of my custom field that appears in the side bar and alter things according to what that field is set to.

    What i’m finding is, if i retrieve the sidebar field in my page.php template, it returns the number, as expected. But if i try from within the hero block, it returns absolutely nothing!

    Any ideas? :^)

  • Anyone know if this is doable?

    It looks like this post wasn’t read yet but i did have a weird issue when i posted it (it threw an error saying it was a duplicate post and i only just now stumbled across the fact it has turned up!)

    Surely i can check a value outside of a block and use it to alter the block?

  • From what I understand you’re working inside an ACF block and want to get data from another block on the same page.

    You can get block data from a page like so:

    
    $post = get_post($post_id);
    $blocks = parse_blocks($post->post_content);
    

    $blocks here will be an array of all blocks in the page, this includes their basic info and all the data (fields).

  • If you’re trying to get fields bound to the page itself (and not another block), you could get it by passing the page id to the get_field function:

    
    $value = get_field('my_field', $post_id);
    
  • Hi JasperV
    That is exactly what i was looking for! Thank you :^)

Viewing 5 posts - 1 through 5 (of 5 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.