Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

Wordpress wp_get_post_terms order by parent not ordering children and grandchildren correctly

I think that the start of my problem is that parent children and grandchildren don't have the ID's in hierarchical order, the parent category was added after children categories so i have problem ordering them correctly.

I want to get the list of custom taxonomies for custom post type, for that i use the function wp_get_post_terms to list the taxonomies. But i can't get them in order of parent > children > grandchildren

From the wordpress documentation this should work:

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

But the grandchildren category is always listed before children category.

This is the categories list:

Services
   -- Design
      -- Artistic

But in the database their id's are not hierarchical

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

And i can't make them display in order Services > Design > Artistic

They always end up in different order.

Here's an array output

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"
  }
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...