I have a custom Taxonomy named 'authors' where I insert additional authors. My question is: is it possible to list all the taxonomy terms order by "Last Name". If I order by name clearly it looks like:

  • Andrea Red
  • Daniel Green
  • Elena Blue

Instead of this I would like to sort by Last Name:

  • Elena Blue
  • Daniel Green
  • Andrea Red

Do you have any advice to do it? Is it better to add a New Custom Field in Custom Taoxnomy where I specify the Last Name and then I sort by that term?

Thank you very much



1 Answer 1


You can try the MySQL function SUBSTRING_INDEX() within the get_terms_orderby filter:

 * Order by the last word in the term name
 * @link https://wordpress.stackexchange.com/a/195039/26350
add_filter( 'get_terms_orderby', function( $orderby, $args )
    if( isset( $args['orderby'] ) && 'wpse_last_word' === $args['orderby'] )  
        $orderby = " SUBSTRING_INDEX( t.name, ' ', -1 ) ";
    return $orderby;
}, 10, 2 );

to order by the last word in the term name.

Here we activate the last-word ordering through our custom wpse_last_word argument:

$terms = get_terms( 'category', [ 'orderby' => 'wpse_last_word' ] );

You can also add the wpse_last_word argument, through the get_terms_args filter, if you need to override some term query.

I recently used this method here for posts.

  • Glad to hear it worked for you @Elena
    – birgire
    Jul 20, 2015 at 15:23
  • @Elena you should accept this answer as it seemed to have worked for you;-). Look for the checkmark next to the answer and click on it, it will go green. Done. ;-) To help you, please see what should I do when someone answers my question Jul 20, 2015 at 15:49
  • Oooh I'm sorry @birgire! I'm new here and I don't know exactly what to do! Thanks for the advice @PieterGoosen! :)
    – Elena
    Jul 21, 2015 at 14:34
  • Hey, I'm working on a similar situation and this solution is very nearly perfect. Any idea how to exclude suffixes (Jr, Sr, II, etc.) so that they are never used for the alphabetization?
    – Josh M
    May 14, 2018 at 17:07
  • 1
    @JoshM maybe my answer here can help or the others there.
    – birgire
    May 14, 2018 at 21:14

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.