Wordpress wp_get_post_terms 由父母订购,但没有正确订购子孙

时间:2021-01-06 00:00:54

标签: wordpress

我认为我的问题的开始是父子和孙子没有按层次顺序排列的 ID,父类别是在子类别之后添加的,所以我在正确排序它们时遇到问题。

我想获取自定义帖子类型的自定义分类法列表,为此我使用函数 wp_get_post_terms 列出分类法。但我无法按照 parent > children > grandchildren

的顺序获取它们

从 wordpress 文档来看,这应该可行:

wp_get_post_terms($edit_post->ID, 'listings_category', array("fields" => "ids", "orderby" => "parent"));

但是孙子类别总是列在子类别之前。

这是类别列表:

Services
   -- Design
      -- Artistic

但是在数据库中他们的id不是分层的

Services => id(454)
Design   => id(43)
Artistic => id(44)

而且我不能让它们按顺序显示 Services > Design > Artistic

它们总是以不同的顺序结束。

这是一个数组输出

array(3) {
  [0]=>
  object(WP_Term)#7074 (10) {
    ["term_id"]=>
    int(494)
    ["name"]=>
    string(8) "Services"
    ["slug"]=>
    string(8) "services"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(494)
    ["taxonomy"]=>
    string(17) "listings_category"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(0)
    ["count"]=>
    int(1)
    ["filter"]=>
    string(3) "raw"
  }
  [1]=>
  object(WP_Term)#7073 (10) {
    ["term_id"]=>
    int(44)
    ["name"]=>
    string(20) "Artistic"
    ["slug"]=>
    string(18) "artistic"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(44)
    ["taxonomy"]=>
    string(17) "listings_category"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(43)
    ["count"]=>
    int(1)
    ["filter"]=>
    string(3) "raw"
  }
  [2]=>
  object(WP_Term)#7075 (10) {
    ["term_id"]=>
    int(43)
    ["name"]=>
    string(23) "Design"
    ["slug"]=>
    string(23) "design"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(43)
    ["taxonomy"]=>
    string(17) "listings_category"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(494)
    ["count"]=>
    int(1)
    ["filter"]=>
    string(3) "raw"
  }
}

0 个答案:

没有答案
相关问题