Sort order using get_the_terms() for multiple taxonomies

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.

Read more here:: Sort order using get_the_terms() for multiple taxonomies

Leave a Reply

Your email address will not be published. Required fields are marked *