Getting WooCommerce categories in the correct order

I’ve had this problem a few times, and I’ve always forgotten how to do it from one project to the next. After looking around (again) at the resources online, I’ve found that there’s not a lot of information out there on how to actually do this and have it work correctly.

It turns out that it’s actually very easy. I know, a lot of people say that, but in this case it’s true. So true that most times when I see the actual solution I don’t believe it.

All you need to do is query the order for the correct term meta value – which in this case is simply ‘order’. Yes, that’s it. Here’s an example:

$categories = get_terms (array (
    'taxonomy' => 'product_cat',
    'hide_empty' => true,
    'orderby' => 'meta_value_num',
    'order' =>'ASC',
    'meta_key' => 'order',
    'parent' => 0

That’s all there is to it. Set up the meta_key as ‘order’ and the orderby value as ‘meta_value_num’, and you’re all set!

Leave a Reply

Your email address will not be published. Required fields are marked *