My final solution to parse for reusable block data also…
if ( function_exists( 'get_field' ) ) {
$pid = get_post();
if ( has_blocks( $pid_content ) ) {
$blocks = parse_blocks( $pid->post_content );
foreach ( $blocks as $block ) {
if ( $block['blockName'] === 'acf/your-block-name' ) {
// Access to block data
} elseif ( $block['blockName'] === 'core/block' ) {
$block_content = parse_blocks( get_post( $block['attrs']['ref'] )->post_content );
if ( $block_content[0]['blockName'] === 'acf/your-block-name' ) {
// Access to "some" block data
}
}
}
}
}
Are you looking for something like this? The below would give you the data of each block item on the page and you can filter through them based on blockName or other information to target only ACF blocks.
if ( function_exists( 'get_field' ) ) {
$pid = get_post();
if ( has_blocks( $pid_content ) ) {
$blocks = parse_blocks( $pid->post_content );
foreach ( $blocks as $block ) {
var_dump( $block );
}
}
}
Keep in mind if you’re using reusable blocks the information isn’t included and only the ref ID is. I’m currently trying to get the ACF data from a ref ID so I can limit something in my footer from displaying unless the block exists on the page.
This issue has also come up for me. I’ve tested both the Duplicate Post and Duplicate Page plugins but the block content doesn’t get duplicated… Just the empty block exists on the new post or page.
Has anyone had success in fixing this issue?
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.