Get_Terms()在自定义帖子类型上提供了错误的自定义分类Childs计数

时间:2021-09-20 作者:pixelcrash

我有一个自定义的分类法,我们称之为;指示;。我们将其与自定义帖子类型“配合使用”;产品;。我们对术语Parent->;小孩父级从未与产品建立连接。我使用它来创建一个自定义的select字段,并且需要父字段作为optgroup标题。

是否使用此结构父级

子级1(根据后端的2篇帖子)子级2(根据后端的1篇帖子)

    但是如果我查询子主题并检查计数,它总是显示为NULL。在Wordpress仪表板中,它使用2和1正确定位。

    为什么不在前端呢。

      $args = array(
              \'hide_empty\' => false, 
              \'orderby\' => \'name\'
      );
          
      $ins = get_terms(\'indikationen\', $args);
    
    if($ins):
        echo "<select>";
        foreach($ins as $in):
    
          
            if ( count( get_term_children( $in->term_id, \'indikationen\' ) ) > 0 ):
            echo \'<optgroup label="\'.$in->name.\'">\';
              foreach($ins as $sub):
                if($sub->parent == $in->term_id):
                    echo "<option value=\'".$sub->term_id."\'>".$sub->name. " count: ".$sub->count."</option>";
              // SUB-COUNT is always NULL but has correct count in backend
                
                endif;
              endforeach;
            echo \'</optgroup>\';
            
            else:
              if($in->count):
                echo "<option value=\'".$in->term_id."\'>" .$in->name. " (".$in->count.")</option>";
              endif;
            endif;  
        
        endforeach;
        echo "</select>";
      endif;
    
    
    
    我做错了什么?

    var_dump($sub);
    // shows me for count also NULL
    
    谢谢

1 个回复
SO网友:pixelcrash

所以我想出了一个解决办法。不确定这是否是最快和最好的解决方案,但它对我们的自定义字段非常有效。

这适用于自定义帖子类型和自定义分类法,并将所有内容排序为A->;Z

Any better solution?

// get all posts from custom post type and their TERMS
// suppress filters is for WPML
      
// update to your custom_post_type

$posts_in_post_type = get_posts( array(
    \'fields\' => \'ids\',
    \'post_type\' => \'custom_post_type\',
    \'posts_per_page\' => -1,
    \'suppress_filters\' => 0
) );

// update custom_taxonomy 
$ins = wp_get_object_terms( $posts_in_post_type, \'custom_taxonomy\', array( \'ids\' ) ); 

// CREATE array to store the values
$cTerms = array();

  
if($ins):

  // First add all parents and also a bool to check if it is a parent or not
  foreach($ins as $in):
    if($in->parent):
      $cTerms[$in->parent][\'name\'] = get_term( $in->parent )->name;
      $cTerms[$in->parent][\'parent\'] = 1;
    endif;
  endforeach;

  // add values - check if the value has a parent or not
  // if has parent add it to [childs]
  foreach($ins as $in):
    if($in->parent):
      $newdata = array($in->term_id => $in->name, "name" => $in->name, "child_id" => $in->term_id);
      $cTerms[$in->parent][\'childs\'][] = $newdata;
    else:
      $cTerms[$in->term_id][\'name\'] = $in->name;
      $cTerms[$in->term_id][\'parent\'] = 0;
    endif;
  endforeach;

  // SORT Main Array
  usort($cTerms, function($a, $b) {
    return $a[\'name\'] <=> $b[\'name\'];
  });

  // SORT Childs
  if($cTerms):
    foreach($cTerms as $cterm):
      if($cterm[\'parent\']):
        usort($cterm[\'childs\'], function($a, $b) {
          return $a[\'name\'] <=> $b[\'name\'];
        });
      endif;
    endforeach;
  endif;


endif;

// possible output as select with parent as optgroup header
echo "<select>";
foreach($cTerms as $key => $menu):

  if($menu[\'parent\']):
    echo \'<optgroup label="\'.$menu[\'name\'].\'">\';      
    if($menu[\'childs\']):
     foreach($menu[\'childs\'] as $child):
       echo \'<option value="\'.$child[\'child_id\'].\'">\'. $child[\'name\'] .\'</option>\';
     endforeach;
     echo \'</optgroup>\';  
    endif;
  else:
    echo \'<option value="\'.$key.\'">\'. $menu[\'name\'] .\'</option>\';
  endif;

endforeach;
echo "</select>";

相关推荐

如何通过‘Get_Terms’获取‘Term’父ID的直接子ID?

我的分类方向如下:Series (ID: 15) (root category)在“系列”下,存在以下子类别:A系列(包含0篇帖子)B系列(包含1篇帖子)C系列(包含0篇帖子)如何使用get_terms\'?我使用了以下参数结构发送为$args (term query)至\'get_terms\' 没有结果。它返回一个空数组:$arg[\'taxonomy\'] = \'category\'; $arg[\'hide_empty\'] = 0; $arg[\'parent\'] = 15;