Hi,
I’ve sent a ticket to support but thought I’d try on here.
Does anyone know if it’s possible to get_field() outside block?
So you can do a unique get_posts(). Then you can parse_blocks().
This will give you an array of block id’s but doesn’t allow you to get_field() still.
Was hoping I can get_field( ‘the_field’, ‘block_id’ ), but it didn’t work, so I cannot see any other way of getting block fields without being inside the block.
Hope that makes sense.
Thanks
Hi, I think that parsing blocks is the only way as the field values are not stored in the database but within the post content.
Look at this:
<!-- wp:acf/overview {"id":"block_5c582992decfd","data":{"my-overview-content":{"my-overview-content-lead":"Here we've got our report overview with all these fancy links."},"my-overview-background":{"my-overview-background-image":""}},"name":"acf/overview","mode":"preview","className":"is-style-default"} /-->
Maybe you can write a nice & useful function get_block_field() and share here. 🙂
I did a quick blog post about that last week, as I needed a quick way to retrieve any block data.
Check it out if you like
Cheers
Alex
Nice, I did something similar in the end as it was the only way. However, I’d urge core to allow/introduce the get_field('field', 'block_id')
method as it makes so much sense and works well with everything else.
@portalzine nice example. Inspired by your example I created this utility class for getting a specific block using block_id and post_id – might be useful for others.
https://gist.github.com/jenssogaard/54a1927ecf51c3238bd3eff1dac73114
@jenssogaard Well done! Thank you!
Following your lead I added recursion to easily access nested blocks.
https://gitlab.com/micqey/acf-block-helper/-/blob/master/acf-block-helper.php
@micqey Thanks for the code – are you able to give an example of use?
These solutions won’t work anymore since ACF has removed the id attribute from the block comment.
See https://www.advancedcustomfields.com/resources/whats-new-with-acf-blocks-in-acf-6/#block-id
The topic ‘Getting get_field outside block loop’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.