Home › Forums › Feature Requests › Sort Taxonomy terms › Reply To: Sort Taxonomy terms
I don’t think there is any automatic way to do this. The way I do it is that I add a field to the term editor page, usually called something like “sort_order”.
Then my code for sorting them will look something like this:
$taxonomy = 'my_taxonomy';
$args = array(); // whatever arguments you're using
$terms = get_terms($taxonomy, $args);
$count = count($terms);
for ($i=0; $i<$count; $i++) {
$terms[$i]->sort_order = get_field('sort_order', $taxonomy.'_'.$terms[$i]->term_id;
}
usort($terms, 'my_sort_terms_function';
the sorting function looks something like this:
function my_sort_terms_function($a, $b) {
// this function expects that items to be sorted are objects and
// that the property to sort by is $object->sort_order
if ($a->sort_order == $b->sort_order) {
return 0;
} elseif ($a->sort_order < $b->sort_order) {
return -1;
} else {
return 1;
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.