I have created several ACF powered Gutenberg blocks and I’m loving it so far. Eliot is making more of a difference for WordPress developers than anyone else I know.
I have a Gutenberg block that contains a simple post loop in the template. The posts have a few custom fields that are used in the template.
The problem is, these custom fields are null once the post loop was moved into a block. I suspect that the get_field()/the_field() functions are scoped to the block itself and no longer scoped to the posts within the loop.
Is this a bug or a limitation to the current beta stage of acf_register_block() and the volatile state of Gutenberg?
Isn’t the get_field() function by default getting the global $post->ID?
You can get the fields within your loop by providing the loop’s post-ID as the second parameter with get_field() to get the it.
You were correct. Adding the post ID fixed the issue.
Just clarifying for future readers with a bit of code! Inside a block, you’d need to force the ID of the parent current post, like this: get_field('myField', get_the_ID())
Thanks for this solution.
Do you know, if there is any change for future updates? I think it is really annoying (for debugging), because it works without the id if you are outside the block.