I ran into a similar situation that I solved with the direction given by Cyril. I needed to sort my taxonomy term “artist” from the post type “items” alphabetically by the custom field “last-name”. I set up the custom field using Elliot Condon’s excellent Advanced Custom Fields plugin. Here is my solution. You will need to adapt it for your own situation.
ENGLISH
1. Get the terms for your taxonomy. The orderby passed to WP’s get_terms function is irrelevant because we will resort the returned PHP object later on.
2. Get the custom field value and append it to your PHP object.
3. Add your comparison function to your functions.php file that you will call for sorting the object.
4. Use PHP’s usort() function to sort your object by the comparison criteria you defined in step 3.
5. Use a foreach loop to output the sorted array in whatever way you need.
That’s it! My code is below. Please let me know if there is any problem with my code. Everything seems to work well for me with my own installation.
CODE
// STEP 1
$artistsList = get_terms( 'artists', $args );
// STEP 2
$t = 0;
// expand the array to include the last name field
foreach ( $artistsList as $artist ) {
$termID = $artistsList[$t]->term_id;
// get the field last name for this ID
$lastName = get_field('last_name', 'artists_'.$termID);
// plop it into the object
$artistsList[$t]->lastName = $lastName;
$t ++;
}
// STEP 3
function cmp($a, $b)
{
return strcmp($a->lastName, $b->lastName);
}
// STEP 4
usort($artistsList, "cmp");
// STEP 5
foreach ( $artistsList as $artist ) {
// code to output your awesome list of taxonomy terms sorted by custom field here
}