Sort order using get_the_terms() for multiple taxonomies

by nickpish   Last Updated March 30, 2018 04:08 AM

I'm using get_the_terms() to echo the term names of multiple taxonomies by passing an array of taxonomy slugs, as follows:

$terms = get_the_terms( get_the_ID(), array('tax-1-slug', 'tax-2-slug') );
if ($terms != null) {
    echo '  <p class="terms">';
    foreach( $terms as $term) {
        echo '      <span class="term">' . $term->name . '</span>';
    echo '  </p>';

This works perfectly fine, but I'm noticing that WP appears to be sorting the term names alphabetically as a single group, and I'd like them output by taxonomy first—thus, in this case, the tax-1-slug terms would be output in alphabetical order, followed by those associated with tax-2-slug and so on. Can this be achieved without having to basically repeat the function multiple times for each taxonomy? I attempted to use ksort($terms), but that didn't seem to help. Thanks for any insight here.

Related Questions

add_action priority and taxonomy used in plugin

Updated July 17, 2015 15:03 PM

WP Tax query & order by tax query not working

Updated July 10, 2017 08:08 AM