The trick here is to create an array out of taxonomy's terms and sort it. Before we do the sorting, we need to divide the full name "John Doe" to seperate strings "John" and "Doe". Then we can sort names by last name.
Rest of the instructions and documentations are in the code.
This should work, but I haven't tested this with actual WP terms, so please test it first.
<?php // Get the taxonomy's terms
$terms = get_terms(
array(
'taxonomy' => 'supporter',
'hide_empty' => false,
)
);
// Check if any term exists
if ( ! empty( $terms ) && is_array( $terms ) ) {
// Create a new empty array of supporters ( $supporters ) which will include first and last names.
$supporters = [];
foreach ( $terms as $term ) {
// Divide the whole name to different words.
// First word ( name_divided[0] ) will be the person's first name
// Second word ( name_divided[1] ) will be the person's last name
$name_divided = explode( ' ', trim($term->name) );
// We'll create an array $supporters which has the following structure:
// $supporters[0] = array (
// 'firstname' => 'John',
// 'lastname' => 'Doe' ),
// )
$supporters[] = array( 'firstname' => $name_divided[0], 'lastname' => $name_divided[1] );
}
// Here's the magic. Sort the array by last name
array_multisort (array_column($supporters, 'lastname'), SORT_ASC, $supporters);
// Run a loop and print them all
foreach ( $supporters as $supporter ) { ?>
<li class="supporter-name"><?php echo $supporter['lastname'] . ', ' . $supporter['firstname']; ?></li>
<?php
}
} ?>