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>';

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>';
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.