• 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"
      }
    }
    
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The parent ordering simply orders terms by the ID value in the parent field, it does not give you hierarchical ordering. To get hierarchical ordering you need some sort of recursive walker function. For guidance, look at how wp_list_categories() manages this. It calls walk_category_tree(), which uses a Walker_Category class to work out hierarchical ordering.

    I believe you can use walk_category_tree() for your own purposes. Despite its name, you can use it for other taxonomy terms. You can pass to it any array of terms you like, it doesn’t have to be all of the existing terms.
    https://developer.www.ads-software.com/reference/functions/walk_category_tree/

Viewing 1 replies (of 1 total)
  • The topic ‘wp_get_post_terms order by parent not ordering children and grandchildren correc’ is closed to new replies.