wp query – Get product_cat terms in the order I set them in Admin area

Question

I want to get an array of the terms (categories) of WC products. But I want to get them in the same order I ordered them in the Admin area, that is in Products > Categories

So, at first I wrote this piece of code and expected it to work:

$categories = get_terms(array( 'taxonomy' => 'product_cat', 'orderby' => 'term_order', 'order' => 'ASC', ));  Then I noticed in the manual that: ‘orderby’ (string) Field(s) to order terms by. Accepts: • term fields (‘name’, ‘slug’, ‘term_group’, ‘term_id’, ‘id’, ‘description’, ‘parent’, ‘term_order’). Unless$object_ids is not
empty, ‘term_order’ is treated the same as ‘term_id’.

So I edited the code above to:

$categories = get_terms(array( 'taxonomy' => 'product_cat', 'object_ids' => get_terms(array( 'taxonomy' => 'product_cat', 'fields' => 'ids', )), 'orderby' => 'term_order', 'order' => 'ASC', ));  but this time I get an empty array… So after even more careful reading I noticed that the bold part of the quote in the manual refers to $object_ids which are obviously different than $term_ids. In fact $object_ids exist as a column (object_id) in wp_term_relationships table and their value is totally different from the next column in the table (term_taxonomy_id)… So what’s the story here?

How do I get the \$object_ids of the terms I’m interested in, in an ordered way? This gets more complicated than I originally anticipated, and it gets frustrating too, because googling the keywords get_terms term_order returns a whole bunch of results on this and other websites, where none of the proposed solutions seem to work. Some of them mention the use of a filter to get_terms_orderby, to forcefully instruct WP to use the desired ordering param, etc…

So, anyone figured this out? TIA.

0
2 months 2021-12-05T17:48:15-05:00 0 Answers 0 views 0