While working on a recent project I needed to find a way to pull a specific piece of content or “Block” out of the post content (if it was present in the content) to use elsewhere within the website to make things a little more streamline for the client and to re-use content that is already present. Traditionally I’d have likely used a specific custom field for this sort of thing but having moved over the the Block Editor I’ve been finding more efficient ways to achieve this, I’ve put an example together below for a general use case.

A recursive function find a specific Block

This function below will search for the specific Block nominated within the function call, first it will search the top level of the_content and then will search through each innerBlock object within the post_content until it finds the specified Block.

<?php
      // Recursively find a specific Block within the_content.
      function wp_find_block( $blocks, $blockName ) {

            foreach ( $blocks as $block ) {

                        // Check if the current Block is the desired block.
                        if ( $blockName === $block['blockName'] ) {

                              // If the Block is a match render the Block content.
                              return render_block( $block );

                        } elseif ( ! empty( $block['innerBlocks'] ) ) {

                              // Recursively run the wp_find_block function to search through the "innerBlocks" array.
                              $inner_blocks = wp_find_blocks( $block['innerBlocks'], $blockName );

                              // If "innerBlocks" contain the desired Block, return its content.
                              if ( $inner_blocks ) {
                                    return $inner_blocks;
                              }
                        }
            }

            // If the block is not found, return an empty string
            return '';
      }
?>

Using the function within your templates

This is a real world use case, building the query for the function with the top level variables and how that then maybe used within your templates, this example searches through the post_content to find a “core/quote” block and then renders out the final result. This specific case is suitable as the client will only ever be using one “Quote” per post, obviously bare in mind if your site is likely to use multiple versions of a specific Block you’ll need to tweak the function to allow for that or limit the items the function will render.

<?php 
      // Get the Post ID.
      $post_ID = get_the_ID();

      // Get the Post Content via the Post ID.
      $post_content = get_the_content( $post_ID );

      // Parse the Post Content for Blocks.
      $blocks = parse_blocks( $post_content );

      // Find the "core/quote" Block within the Post Content.
      $quote_block = wp_find_block( $blocks, 'core/quote' );
?>


<article id="<?php echo $post_ID; ?>" <?php post_class(); ?>>

      <h3><?php the_title(); ?></h3>

      <?php
            if( ! empty($quote_block) ){
                  echo $quote_block;
            }
      ?>

      <a href="<?php the_permalink(); ?>">Read Article</a>

</article>