Support

Account

Home Forums Gutenberg Getting get_field outside block loop

Solving

Getting get_field outside block loop

  • 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

    • macemmek

    • February 16, 2019 at 12:04 am

    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

    • micqey

    • August 26, 2020 at 4:01 am

    @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

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