Site icon Hip-Hop Website Design and Development

How to makes Custom Post Type posts as Gutenberg’s patterns?

I’ve created custom post type ml_patterns_. Now, I want to use each of post as a separate Gutenberg Block Pattern. So for that I’ve created this:

$args = array(  
    'post_type' => 'ml_patterns_',
    'post_status' => 'publish',
    'posts_per_page' => 9999, 
    'orderby' => 'title', 
    'order' => 'ASC', 
);

$loop = new WP_Query( $args ); 
    
while ( $loop->have_posts() ) : $loop->the_post();

    add_action( 'init', function() {
        $title = __( get_the_title(), 'ml_patterns' ); // post's name is the title of the pattern (label)
        $titleSlug = 'ml_patterns/ml_pattern_'.get_the_ID(); // post's slug is the name of the pattern
        $patternContent = get_the_content(); // the post content as pattern content
        register_block_pattern(
            $titleSlug,
            array(
                'title'       => $title,    
                'content'     => trim($patternContent),
                'categories'  => array( 'ml_test' ),
            )
        );
    });

endwhile;

wp_reset_postdata();

works fine but I se only one pattern, even if there is more than one post. Where is my mistake?