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>';
1 Answer
Reset to default 0Don'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>';