Site icon Hip-Hop Website Design and Development

How to create Gutenberg’s pattern category automatically with custom post taxonomy terms?

I’m trying to create pattern category form each custom post taxonomy term that is assigned to post in custom post type ml_patterns_. Actually I have 3 posts with different terms.

Code:

$patternsTerms = get_terms('ml_patternsCustomTaxonomy', array('hide_empty' => 0, 'parent' =>0)); 
    foreach($patternsTerms as $patternTerm):
    
      $slug = $patternTerm->slug;
      $name = $patternTerm->name;
    
      register_block_pattern_category(
          $slug,
            array(
              'label' => $name,
          )
        );
    
    endforeach;

the results is one pattern category Uncategorized which is event not created in this taxonomy. To test it I’ve tried this, on single page:

$patternsTerms = get_terms('ml_patternsCustomTaxonomy', array('hide_empty' => 0, 'parent' =>0)); 
foreach($patternsTerms as $patternTerm):

    $slug = $patternTerm->slug;
    $name = $patternTerm->name;

    echo $slug;
    echo '</br>';
    echo $name;
    echo '<hr>';

endforeach;

and here it’s works ok. Any idea? 🙂