I'm using WordPress' the_tags() to display the tags of each post on a single side. What I intend to do is to display the tags by the order they are typed in in the backend, and not by alphabetical order. The WordPress codex doesn't seem to have an argument to sort the_tags() and the output is always in alphabetical order.

Does anyone of you have a tip?

1 Answer 1


Follow the White Rabbit:

  1. the_tags()
  2. get_the_tag_list()
  3. get_the_term_list()
  4. get_the_terms()
  5. wp_get_object_terms()

If you look through that last function, you should see this:

$defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');

You should have alphabetical order by default. And no, there are no filters I see that would allow you to directly alter the query, but there are several filters along that rabbit hole. The wp_get_object_terms filter looks like a good one to me.

// you will probably need to run this once to reset the cache
// wp_cache_flush();

function id_ordered_tags_wpse_144703($terms, $object_ids, $taxonomies, $args) {

  $taxonomies = explode(',',$taxonomies); // quoted and comma seperated string

  if (1 < count($taxonomies) && !in_array("'post_tag'",$taxonomies)) return $terms;

  $sorted = array();
  foreach ($terms as $term) {
    $sorted[$term->term_id] = $term;


  return $sorted;

// test it

Where you add that code depends on context. In function.php would work but would work but would be restricted only to the theme you add the code to, which I am guessing is correct in this case. Don't include the the_tags() line. That is just for testing.

A plugin file or mu-plugin file would allow the filter to work for any theme. I doubt that is the intent, and it strikes me a bit unfriendly anyway as it. Again, don't include the the_tags() line.

You can actually add the code exactly as written in the spot you want to use the_tags(), though that is a messy way to do it.

The code hooks into WordPress Core functions. It will run whenever the wp_get_object_terms filter runs. You don't have to do anything special besides add the filter callback to the queue, which you do with add_filter.

You can get more control over when the filter runs by adding it when you need it and removing it afterwards:

// test it
  • I really like your entertaining way to give answers, but since I'm not a wordpress expert I still have to annoy you with my basic questions. Where would I put the id_ordered_tags_wpse_144703function. And would this function be called by still using the_tags(). I'm a wordpress beginner and have a touch of php knowledge. Thank you!
    – user49869
    May 18, 2014 at 16:03
  • @user49869 : see the edit.
    – s_ha_dum
    May 18, 2014 at 16:09
  • unfortunenately it does not work. I made an own very simple theme and put the following in the functions.php between <?php your code ?> and then in my single.php where I want to get the tags just the_tags(). But it does not display anything. its just blank (the whole single.php)
    – user49869
    May 18, 2014 at 16:30
  • You have a syntax error... probably the <?php and '?>' tags.
    – s_ha_dum
    May 18, 2014 at 16:49
  • sir, I appreciate the help. Works like a charm! Thank you!
    – user49869
    May 18, 2014 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.