• I’ve been trying all night to get this to work, and it doesn’t seem to want to… it works perfectly when i do it with wp_list_categories() but i dont want a html list, i want an array to use in my code… using get_categories() does not return a hierarchical array to use.

    here’s my code and output with wp_list_categories()
    wp_list_categories( array(''hide_empty' => 0, 'hierarchical' => 1, 'taxonomy' => 'locations') );

    • United States
    • Alabama
    • Birmingham
    • Huntsville
    • Mobile
    • Montgomery
    • Alaska
    • Anchorage
    • Fairbanks
    • Juneau
    • Arizona
    • Phoenix
    • Tucson
    • Arkansas
    • Fort Smith
    • Little Rock

    But when i do that with print_r() on the get_categories() i get this

    $allcats=get_categories( array('hide_empty' => 0, 'hierarchical' => 1, 'taxonomy' => 'locations'));
    print_r($allcats);

    and the output is

    Array
    (
        [0] => stdClass Object
            (
                [term_id] => 30
                [name] => Alabama
                [slug] => al
                [term_group] => 0
                [term_taxonomy_id] => 30
                [taxonomy] => Locations
                [description] =>
                [parent] => 27
                [count] => 0
                [cat_ID] => 30
                [category_count] => 0
                [category_description] =>
                [cat_name] => Alabama
                [category_nicename] => al
                [category_parent] => 27
            )
    
        [1] => stdClass Object
            (
                [term_id] => 106
                [name] => Alaska
                [slug] => alaska
                [term_group] => 0
                [term_taxonomy_id] => 106
                [taxonomy] => Locations
                [description] =>
                [parent] => 27
                [count] => 0
                [cat_ID] => 106
                [category_count] => 0
                [category_description] =>
                [cat_name] => Alaska
                [category_nicename] => alaska
                [category_parent] => 27
            )
    
        [2] => stdClass Object
            (
                [term_id] => 171
                [name] => Anchorage
                [slug] => anchorage
                [term_group] => 0
                [term_taxonomy_id] => 171
                [taxonomy] => Locations
                [description] =>
                [parent] => 106
                [count] => 0
                [cat_ID] => 171
                [category_count] => 0
                [category_description] =>
                [cat_name] => Anchorage
                [category_nicename] => anchorage
                [category_parent] => 106
            )
    
        [3] => stdClass Object
            (
                [term_id] => 108
                [name] => Arizona
                [slug] => arizona
                [term_group] => 0
                [term_taxonomy_id] => 108
                [taxonomy] => Locations
                [description] =>
                [parent] => 27
                [count] => 0
                [cat_ID] => 108
                [category_count] => 0
                [category_description] =>
                [cat_name] => Arizona
                [category_nicename] => arizona
                [category_parent] => 27
            )
    
        [4] => stdClass Object
            (
                [term_id] => 118
                [name] => Arkansas
                [slug] => arkansas
                [term_group] => 0
                [term_taxonomy_id] => 118
                [taxonomy] => Locations
                [description] =>
                [parent] => 27
                [count] => 0
                [cat_ID] => 118
                [category_count] => 0
                [category_description] =>
                [cat_name] => Arkansas
                [category_nicename] => arkansas
                [category_parent] => 27
            )
    
        [5] => stdClass Object
            (
                [term_id] => 52
                [name] => Birmingham
                [slug] => birmingham
                [term_group] => 0
                [term_taxonomy_id] => 52
                [taxonomy] => Locations
                [description] =>
                [parent] => 30
                [count] => 0
                [cat_ID] => 52
                [category_count] => 0
                [category_description] =>
                [cat_name] => Birmingham
                [category_nicename] => birmingham
                [category_parent] => 30
            )
    
        [6] => stdClass Object
            (
                [term_id] => 172
                [name] => Fairbanks
                [slug] => fairbanks
                [term_group] => 0
                [term_taxonomy_id] => 172
                [taxonomy] => Locations
                [description] =>
                [parent] => 106
                [count] => 0
                [cat_ID] => 172
                [category_count] => 0
                [category_description] =>
                [cat_name] => Fairbanks
                [category_nicename] => fairbanks
                [category_parent] => 106
            )
    
        [7] => stdClass Object
            (
                [term_id] => 158
                [name] => Fort Smith
                [slug] => fort-smith
                [term_group] => 0
                [term_taxonomy_id] => 158
                [taxonomy] => Locations
                [description] =>
                [parent] => 118
                [count] => 0
                [cat_ID] => 158
                [category_count] => 0
                [category_description] =>
                [cat_name] => Fort Smith
                [category_nicename] => fort-smith
                [category_parent] => 118
            )
    
        [8] => stdClass Object
            (
                [term_id] => 70
                [name] => Huntsville
                [slug] => huntsville
                [term_group] => 0
                [term_taxonomy_id] => 70
                [taxonomy] => Locations
                [description] =>
                [parent] => 30
                [count] => 0
                [cat_ID] => 70
                [category_count] => 0
                [category_description] =>
                [cat_name] => Huntsville
                [category_nicename] => huntsville
                [category_parent] => 30
            )
    
        [9] => stdClass Object
            (
                [term_id] => 170
                [name] => Juneau
                [slug] => juneau
                [term_group] => 0
                [term_taxonomy_id] => 170
                [taxonomy] => Locations
                [description] =>
                [parent] => 106
                [count] => 0
                [cat_ID] => 170
                [category_count] => 0
                [category_description] =>
                [cat_name] => Juneau
                [category_nicename] => juneau
                [category_parent] => 106
            )
    
        [10] => stdClass Object
            (
                [term_id] => 157
                [name] => Little Rock
                [slug] => little-rock
                [term_group] => 0
                [term_taxonomy_id] => 157
                [taxonomy] => Locations
                [description] =>
                [parent] => 118
                [count] => 0
                [cat_ID] => 157
                [category_count] => 0
                [category_description] =>
                [cat_name] => Little Rock
                [category_nicename] => little-rock
                [category_parent] => 118
            )
    
        [11] => stdClass Object
            (
                [term_id] => 69
                [name] => Mobile
                [slug] => mobile
                [term_group] => 0
                [term_taxonomy_id] => 69
                [taxonomy] => Locations
                [description] =>
                [parent] => 30
                [count] => 0
                [cat_ID] => 69
                [category_count] => 0
                [category_description] =>
                [cat_name] => Mobile
                [category_nicename] => mobile
                [category_parent] => 30
            )
    
        [12] => stdClass Object
            (
                [term_id] => 72
                [name] => Montgomery
                [slug] => montgomery
                [term_group] => 0
                [term_taxonomy_id] => 72
                [taxonomy] => Locations
                [description] =>
                [parent] => 30
                [count] => 0
                [cat_ID] => 72
                [category_count] => 0
                [category_description] =>
                [cat_name] => Montgomery
                [category_nicename] => montgomery
                [category_parent] => 30
            )
    
        [13] => stdClass Object
            (
                [term_id] => 152
                [name] => Phoenix
                [slug] => phoenix
                [term_group] => 0
                [term_taxonomy_id] => 152
                [taxonomy] => Locations
                [description] =>
                [parent] => 108
                [count] => 0
                [cat_ID] => 152
                [category_count] => 0
                [category_description] =>
                [cat_name] => Phoenix
                [category_nicename] => phoenix
                [category_parent] => 108
            )
    
        [14] => stdClass Object
            (
                [term_id] => 153
                [name] => Tucson
                [slug] => tucson
                [term_group] => 0
                [term_taxonomy_id] => 153
                [taxonomy] => Locations
                [description] =>
                [parent] => 108
                [count] => 0
                [cat_ID] => 153
                [category_count] => 0
                [category_description] =>
                [cat_name] => Tucson
                [category_nicename] => tucson
                [category_parent] => 108
            )
    
        [15] => stdClass Object
            (
                [term_id] => 27
                [name] => United States
                [slug] => us
                [term_group] => 0
                [term_taxonomy_id] => 27
                [taxonomy] => Locations
                [description] =>
                [parent] => 0
                [count] => 1
                [cat_ID] => 27
                [category_count] => 1
                [category_description] =>
                [cat_name] => United States
                [category_nicename] => us
                [category_parent] => 0
            )
    
    )

    it seems that it is sorting by “cat_name” ASC instead of making a hierarchical array…

The topic ‘custom taxonomy hierarchical array/object?’ is closed to new replies.