$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom taxonomy - Order get_terms() by hierarchy|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom taxonomy - Order get_terms() by hierarchy

matteradmin8PV0评论

I am trying to query a custom taxonomy terms in a way that keeps the hierarchical structure.

Here is the code that I am using..

function ev_test_data() {
    $title = 'Mens Levi\'s Jeans';
    $titles = str_replace( array("'", '"', '-', '\\' ), '', explode( ' ', trim($title)) );
    $cats = array();
    foreach ( $titles as $kw ) {
        $terms = get_terms( array(
            'taxonomy' => 'product_cat',
            'orderby' => 'parent', // I guess this is where I am lost.
            'hide_empty' => false,
            'name__like' => $kw,
        ) );
        foreach ( $terms as $term ) {
            $cats[] = $term->term_id;
        }
    }

    $finals = array();
    foreach ( $cats as $cat ) {
        $catobj = get_term_by( 'id', $cat, 'product_cat' );
        $tree = '';
        $ancentors = array_reverse(get_ancestors( $cat, 'product_cat', 'taxonomy' ), true);
        $i=1;
        foreach ( $ancentors as $ancentor ) {
            $sterm = get_term_by( 'id', $ancentor, 'product_cat' );
            $tree .= $i == 1 ? '<b>' . $sterm->name . '</b>' : ' > ' . $sterm->name;
            $i++;
        }
        $tree .= ' > ' . $catobj->name;
        $finals[$cat] = $tree;
    }

    echo '<pre>' . print_r( $finals, true ) . '</pre>'; 
}  

This produces this list.

Array
(
    [4638] => Fashion > Womens Clothing
    [4699] => Fashion > Womens Handbags & Bags
    [4709] => Fashion > Womens Shoes
    [4461] => Fashion > Mens Accessories
    [4493] => Fashion > Mens Clothing
    [4512] => Fashion > Mens Shoes
    [4603] => Fashion > Womens Accessories
    [4779] => Fashion > Vintage > Mens Vintage Clothing
    [4821] => Fashion > Vintage > Womens Vintage Clothing
    [4290] => Fashion > Kids Clothing, Shoes & Accs > Boys Clothing (Sizes 4 & Up) > Jeans
    [4319] => Fashion > Kids Clothing, Shoes & Accs > Girls Clothing (Sizes 4 & Up) > Jeans
    [4354] => Fashion > Kids Clothing, Shoes & Accs > Unisex Clothing > Jeans
    [4500] => Fashion > Mens Clothing > Jeans
    [4660] => Fashion > Womens Clothing > Jeans
    [4666] => Fashion > Womens Clothing > Maternity > Jeans
)  

However, this list is not ordered as I want. I need it to be ordered by the hierarchical order. Something like this.

Array
(
    [4493] => Fashion > Mens Clothing
    [4500] => Fashion > Mens Clothing > Jeans
    [4638] => Fashion > Womens Clothing
    [4660] => Fashion > Womens Clothing > Jeans
    [4666] => Fashion > Womens Clothing > Maternity > Jeans
    [4699] => Fashion > Womens Handbags & Bags
    [4709] => Fashion > Womens Shoes
    [4603] => Fashion > Womens Accessories
    [4512] => Fashion > Mens Shoes
    [4461] => Fashion > Mens Accessories
    [4779] => Fashion > Vintage > Mens Vintage Clothing
    [4821] => Fashion > Vintage > Womens Vintage Clothing
    [4290] => Fashion > Kids Clothing, Shoes & Accs > Boys Clothing (Sizes 4 & Up) > Jeans
    [4319] => Fashion > Kids Clothing, Shoes & Accs > Girls Clothing (Sizes 4 & Up) > Jeans
    [4354] => Fashion > Kids Clothing, Shoes & Accs > Unisex Clothing > Jeans

)

Can this be done using WordPress built in functions? If not, what would be the logic for the custom query? I am not asking to do it for me, but any help or guide or headstart would be great.

Thanks

I am trying to query a custom taxonomy terms in a way that keeps the hierarchical structure.

Here is the code that I am using..

function ev_test_data() {
    $title = 'Mens Levi\'s Jeans';
    $titles = str_replace( array("'", '"', '-', '\\' ), '', explode( ' ', trim($title)) );
    $cats = array();
    foreach ( $titles as $kw ) {
        $terms = get_terms( array(
            'taxonomy' => 'product_cat',
            'orderby' => 'parent', // I guess this is where I am lost.
            'hide_empty' => false,
            'name__like' => $kw,
        ) );
        foreach ( $terms as $term ) {
            $cats[] = $term->term_id;
        }
    }

    $finals = array();
    foreach ( $cats as $cat ) {
        $catobj = get_term_by( 'id', $cat, 'product_cat' );
        $tree = '';
        $ancentors = array_reverse(get_ancestors( $cat, 'product_cat', 'taxonomy' ), true);
        $i=1;
        foreach ( $ancentors as $ancentor ) {
            $sterm = get_term_by( 'id', $ancentor, 'product_cat' );
            $tree .= $i == 1 ? '<b>' . $sterm->name . '</b>' : ' > ' . $sterm->name;
            $i++;
        }
        $tree .= ' > ' . $catobj->name;
        $finals[$cat] = $tree;
    }

    echo '<pre>' . print_r( $finals, true ) . '</pre>'; 
}  

This produces this list.

Array
(
    [4638] => Fashion > Womens Clothing
    [4699] => Fashion > Womens Handbags & Bags
    [4709] => Fashion > Womens Shoes
    [4461] => Fashion > Mens Accessories
    [4493] => Fashion > Mens Clothing
    [4512] => Fashion > Mens Shoes
    [4603] => Fashion > Womens Accessories
    [4779] => Fashion > Vintage > Mens Vintage Clothing
    [4821] => Fashion > Vintage > Womens Vintage Clothing
    [4290] => Fashion > Kids Clothing, Shoes & Accs > Boys Clothing (Sizes 4 & Up) > Jeans
    [4319] => Fashion > Kids Clothing, Shoes & Accs > Girls Clothing (Sizes 4 & Up) > Jeans
    [4354] => Fashion > Kids Clothing, Shoes & Accs > Unisex Clothing > Jeans
    [4500] => Fashion > Mens Clothing > Jeans
    [4660] => Fashion > Womens Clothing > Jeans
    [4666] => Fashion > Womens Clothing > Maternity > Jeans
)  

However, this list is not ordered as I want. I need it to be ordered by the hierarchical order. Something like this.

Array
(
    [4493] => Fashion > Mens Clothing
    [4500] => Fashion > Mens Clothing > Jeans
    [4638] => Fashion > Womens Clothing
    [4660] => Fashion > Womens Clothing > Jeans
    [4666] => Fashion > Womens Clothing > Maternity > Jeans
    [4699] => Fashion > Womens Handbags & Bags
    [4709] => Fashion > Womens Shoes
    [4603] => Fashion > Womens Accessories
    [4512] => Fashion > Mens Shoes
    [4461] => Fashion > Mens Accessories
    [4779] => Fashion > Vintage > Mens Vintage Clothing
    [4821] => Fashion > Vintage > Womens Vintage Clothing
    [4290] => Fashion > Kids Clothing, Shoes & Accs > Boys Clothing (Sizes 4 & Up) > Jeans
    [4319] => Fashion > Kids Clothing, Shoes & Accs > Girls Clothing (Sizes 4 & Up) > Jeans
    [4354] => Fashion > Kids Clothing, Shoes & Accs > Unisex Clothing > Jeans

)

Can this be done using WordPress built in functions? If not, what would be the logic for the custom query? I am not asking to do it for me, but any help or guide or headstart would be great.

Thanks

Share Improve this question edited Jan 9, 2019 at 8:22 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Jan 8, 2019 at 16:33 AbhikAbhik 2,9212 gold badges24 silver badges31 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

I was looking for the same thing and think this should work for you : Custom taxonomy, get_the_terms, listing in order of parent > child

You can use wp_list_categories taxonomy argument which has built in support for hierarchal lists.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far