Home Forums Feature Requests Sort Taxonomy terms


Sort Taxonomy terms

  • Hi,

    I need to sort the taxonomy terms I get from a Taxonomy custom field.
    I have multiple selected term objects and I was just wondering if it’s possible to use the ‘orderby’ parameter or something similar.

    I’ve found this but I don’t know how to apply to the Basic display (multiple values).


  • 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;
  • Hi,
    thanks for your solution.
    After searching how to sort taxonomy terms in WordPress I haven’t found any solution except to use a plugin, so I opted for this plugin:
    and after changing the order I can have the taxonomy terms in the Taxonomy field sorted as I want.


    • nikkaG

    • January 6, 2015 at 7:22 am

    Thanks Hube2,
    your solution works like a charm!
    Before I didn’t know how to re-order an array of terms objects.

  • A solution I came up with, was to use a repeater field and then within that repeater, use a single choice taxonomy select.

    The user can then select a single taxonomy of your choice and reorder them in the repeater.

    It’s not the nicest solution, as they could also add the same taxonomy’s, but hey-ho!

  • In the new version of ACF (5.5.x) now uses term meta and WP allows meta queries for terms, so these work-arounds should no longer be needed.

  • Hi John,

    Could you please advise in light of your suggestion above with 5.5.x as to how I can modify the below to change the ordering?

    $sub_categories = get_field('featured_categories');
    foreach ($sub_categories as $sub_category):
        echo $sub_category->name;

    sub-category is a Taxonomy field with the return value as Term Object

  • @cargoodrich since it’s a taxonomy field you would need to use the filter provided by the taxonomy field to alter the call to get_terms To be honest I don’t know the exact solution.

  • Indeed, it’s not possible with get_terms (get_categories) args, cause final query contains “INNER JOIN wp_termmeta” but acf doesn’t use this table

    So loop through it with usort()

    the shortest variant for now is

    usort($terms, function($a. $b) use ($taxonomy) {
       return get_field('sort_order', $taxonomy.'_'.$a->term_id) - get_field('sort_order', $taxonomy.'_'.$b->term_id);
Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Sort Taxonomy terms’ is closed to new replies.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.