0

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? :)

4
  • Where are you running the first code? Make sure it's inside an init action's callback, so it's not running too early, if it's in your functions.php file. The get_terms() usually takes only a single input array argument, but this might still work the old way. Make sure you're not clashing with existing categories by using relevant namespace.
    – birgire
    Nov 20 '21 at 21:52
  • Yes, it's in functions.php. I've wrapped this code with add_action('init', function() { // } but no expected results.
    – D_P
    Nov 20 '21 at 21:58
  • Try e.g. registering categories manually there without get_terms() loop, and verify the bool output of register_block_pattern_category() is true
    – birgire
    Nov 20 '21 at 22:00
  • I'm also vary of uppercase letters in ml_patternsCustomTaxonomy and there's also a maximum number of taxonomy names letters, don't remember what it is now. But since you verified it, then it might work, but these are typical problems with various keys in WP :-).
    – birgire
    Nov 20 '21 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.