I need to order parent terms by their children count:
function sort_terms_by_children_count ( $terms ) {
$sort_terms_by_children_count = array();
foreach($terms as $term) {
$count = count (get_terms( $terms, array( 'child_of' => $term->term_id, 'hide_empty' => false, ) ));
$sort_terms_by_children_count[$count] = $term;
}
sort($sort_terms_by_children_count);
return $sort_terms_by_children_count;
}
and use it like:
$terms = get_terms('product_cat');
$terms = $sort_terms_by_children_count($terms);
It doesn't work, outputs only one term.
I need to order parent terms by their children count:
function sort_terms_by_children_count ( $terms ) {
$sort_terms_by_children_count = array();
foreach($terms as $term) {
$count = count (get_terms( $terms, array( 'child_of' => $term->term_id, 'hide_empty' => false, ) ));
$sort_terms_by_children_count[$count] = $term;
}
sort($sort_terms_by_children_count);
return $sort_terms_by_children_count;
}
and use it like:
$terms = get_terms('product_cat');
$terms = $sort_terms_by_children_count($terms);
It doesn't work, outputs only one term.
Share Improve this question edited Sep 18, 2019 at 13:53 antonimac asked Sep 18, 2019 at 11:01 antonimacantonimac 112 bronze badges 1- Welcome to WordPress Development. I hope you find the answer(s) you are looking for. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. – Matthew Brown aka Lord Matt Commented Sep 18, 2019 at 13:23
1 Answer
Reset to default 1there should be no need for this function, you could use $args passed to the get_terms. See: https://codex.wordpress/es:Function_Reference/get_terms
get_terms('product_cat', array('orderby' => 'count'));
Cheers!