• Resolved JacoboPolavieja

    (@jacobopolavieja)


    Hello!

    I’m trying to retrieve the hierarchy of categories (default WP taxonomy) a post belongs to (normal blog post, not custom post type).
    I’ve tried every function possible but the order in which they are retrieve, seems pretty much random.

    I have the following tree of categories:
    – Perros (dogs)
    — Razas (breeds)
    —— Rottweiler
    – Gatos (cats)
    — Razas (breeds)
    —— Esfinge / Sphynx

    For the post about Rottweiler I get the hierarchy with this order:
    Perros (dogs) > Razas (breeds) > Rottweiler
    (as defined in the dashboard).
    For the post about the Esfinge / Sphynx I get the hierarchy with this order:
    Esfinge / Sphynx > Gatos (cats) > Razas (breeds)

    So they are in completely different order. I’ve tried their retrieval with every function I’ve come up:
    – get_the_category()
    – wp_get_object_terms
    – get_the_terms

    Only a combination of ‘wp_list_categories’ and ‘wp_list_categories’ seems to do the trick.

    $taxonomy = 'category';
    	
    // Get the term IDs assigned to post.
    $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
    		
    // Separator between links.
    $separator = ', ';
    		
    if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {
    		
    	$term_ids = implode( ',' , $post_terms );
    		
    	$terms = wp_list_categories( array(
    	   'title_li' => '',
    	   'style'    => 'none',
    	   'echo'     => false,
    	   'taxonomy' => $taxonomy,
    	   'include'  => $term_ids
    	) );
    		
    	$terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );
    		
    	// Display post categories.
    	echo  $terms;
    }

    Is this expected? Why doesn’t it retrieve them in the defined hierarchical order (or any other consistent order) by default?

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,
    You can try with the below code.
    You can add your own markup

    function tg_get_tax_hierarchy order() {
        $out = '';
    
        global $post;
    
        $taxonomies = get_object_taxonomies( $post );
    
        foreach ( $taxonomies as $taxonomy ) {  
            $taxonomy = get_taxonomy( $taxonomy );  
            if ( $taxonomy->query_var && $taxonomy->hierarchical ) {
    
                $out .= '<div class="entry-categories">';
                    $out .= '<p>' . $taxonomy->labels->name . '</p>';
    
                    $terms = wp_get_post_terms( $post->ID, $taxonomy->name, array( 'orderby' => 'term_id' ) );
                    foreach ( $terms as $term ) {
    
                        $out .= $term->name;
    
                    }
                $out .= '</div>';
            }
        }
    
        return $out;
    }

    Thanks.

    Thread Starter JacoboPolavieja

    (@jacobopolavieja)

    @themesgrove,

    Thanks, but that code has the problem of ordering by ‘term_id’. I had tried that too, but the problem is that a subcategory can have (I admit is not a usual case) a child with a lower ID than its parent (in case the user creates the child category first, then the parent, then assigns the child to the parent).
    So in that case, it doesn’t display the proper hierarchy.

    I’m amazed there isn’t a standard way to retrieve the hierarchy of categories a post belongs to (or maybe I haven’t found it yet despite all my research).

    Thanks anyway!

    Moderator bcworkz

    (@bcworkz)

    The wp_list_* functions are the ones that output hierarchies. Most functions do not list hierarchies by default because such a list requires a lot of recursive processing and there are functions available to do so, so why repeat the functionality more than necessary?

    One can generically output a hierarchical list by using a Walker class object. wp_list_categories() uses the Walker_Category class. You can extend the generic Walker class in the same way to meet your specific needs.

    Thread Starter JacoboPolavieja

    (@jacobopolavieja)

    Thanks a lot @bcworkz!

    That clarifies everything ; )

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Getting sorted hierarchy of categories’ is closed to new replies.