最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

terms - Order get_terms by count using a custom taxonomy hierarchy

programmeradmin2浏览0评论

When I test get_terms with normal Wordpress posts it seems to be working. Maybe because I don't have hierarchy yet for these categories. But it doesn't work with WooCommerces custom taxonomy "product_cat" when ordering by anything. Im trying to order by count and it returns them by name.

These product categories are nested 3 layers deep in the category tree. Maybe that effects it?

$cats = get_terms(array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'orderby' => 'count', 'order' => 'DESC', 'number' => 5, )); echo '<pre>'; print_r($cats); echo '</pre>';

When I test get_terms with normal Wordpress posts it seems to be working. Maybe because I don't have hierarchy yet for these categories. But it doesn't work with WooCommerces custom taxonomy "product_cat" when ordering by anything. Im trying to order by count and it returns them by name.

These product categories are nested 3 layers deep in the category tree. Maybe that effects it?

$cats = get_terms(array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'orderby' => 'count', 'order' => 'DESC', 'number' => 5, )); echo '<pre>'; print_r($cats); echo '</pre>';

Share Improve this question edited May 17, 2020 at 12:39 bghouse asked May 13, 2020 at 22:22 bghousebghouse 1014 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

Don't know if it's the best solution but I ended up using a custom query to show all categories ordered by count.

$cats = $wpdb->get_results("
    select tt.term_id, count, name, slug
    from ".$wpdb->prefix."term_taxonomy as tt
    inner join ".$wpdb->prefix."terms as terms
    on tt.term_id = terms.term_id
    where taxonomy = 'product_cat' 
    order by count desc limit 10
");
echo '<pre>'; print_r($cats); echo '</pre>';
发布评论

评论列表(0)

  1. 暂无评论