get_terms custom order

This can be easily done with your own custom function. What you want to do here is, get your object from get_terms() which will hold your term objects, check if your unique key is set as a parameter and then according to that, remove that key/pair and add it to the back

Now, lets put that into code: (I have commented the code to make it easy to follow)

This is the pre PHP 5.5 version but still need at least PHP 5.4

function get_term_to_end( $taxonomy = '', $args = [] )
{
    $terms = get_terms( $taxonomy, $args );

    /*
     * Return $terms if our new argument term_to_end is not set or empty
     */
    if ( !isset( $args['term_to_end'] ) || empty( $args['term_to_end'] ) ) 
        return $terms;

    /*
     * If term_to_end is set and has a value, continue to process
     * Return $terms if a wp error is returned or if $terms is empty
     */
    if ( is_wp_error( $terms ) || empty( $terms ) )
        return $terms;
     /*
      * We have came this far, now we can finish this off
      */
    foreach ( $terms as $term ) {
        /*
         * Check the term ids against our new argument term_to_end
         */
        if ( $term->term_id == $args['term_to_end'] ) {
            $end[] = $term;
        } else {
            $terms_array[] = $term;
        }
    }
    /*
     * Merge the two arrays together, adding the term_to_end right at the back
     */
    $terms = array_merge( $terms_array, $end );

    /*
     * For uniformaty, type cast the resultant array to an object
     */
    return (object) $terms;
}

If you are using PHP 5.5 +, you can use array_search and array_column and skip the foreach loop inside the function

function get_term_to_end( $taxonomy = '', $args = [] )
{
    $terms = get_terms( $taxonomy, $args );

    /*
     * Return $terms if our new argument term_to_end is not set or empty
     */
    if ( !isset( $args['term_to_end'] ) || empty( $args['term_to_end'] ) ) 
        return $terms;

    /*
     * If term_to_end is set and has a value, continue to process
     * Return $terms if a wp error is returned or if $terms is empty
     */
    if ( is_wp_error( $terms ) || empty( $terms ) )
        return $terms;

     /*
      * We have came this far, now we can finish this off
      *
      * We need to convert the multidimensional objects to a multidimensional array. Lets used
      * json_encode and json_decode. Now we can use 
      * array_search to and array_column to determine the position of the term_to_end
      */
    $terms_array = json_decode( json_encode( $terms ), true );
    $end_term_position = array_search( $args['term_to_end'], array_column( $terms_array, 'term_id'));

    /*
     * First check if $end_term_position in not false (array_search returns false on failure), 
     * if false, return $terms
     */
    if ( !$end_term_position )
        return $terms;

    /*
     * Get the key value pair for term_to_end, unset it from $terms and reset term_to_end pair at the back
     */
    $end_term_pair[] = $terms[$end_term_position];
    unset( $terms[$end_term_position] );
    $new_terms_array = array_merge( (array) $terms, $end_term_pair );

    /*
     * We are done!! Now we can just return $new_terms_array as an object
     */
    return (object) $new_terms_array;
}

Example use case:

Move term with id 15 belonging to the taxonomy mytax to the back of the term array. Keep the default odering by name

$terms = get_term_to_end( 'mytax', ['term_to_end' => 15] );

Or

$args = [
    'term_to_end' => 15
];
$terms = get_term_to_end( 'mytax, $args );

FEW NOTES ON THE ABOVE

  • The code above is all untested and might be buggy. I have written this on a tablet.

  • For older versions (pre PHP 5.4), use the first code block and simply change the new array syntax ([]) to the old syntax (array())

  • The new parameter, term_to_end uses the term to move to the end term’s ID. You can modify this to work with either the term name or term slug, whatever suites your need. You can also modify this to work with all of these values

  • The new function uses the exact same parameters as get_terms(). The only extra addition is the term_to_end parameter

  • As get_terms, the new function returns a WP_Error object if a taxonomy does not exists and also returns an ampty array if no terms are found. Remember to check for these occurances in the same way you would with get_terms

  • If the term_to_end parameter is set, the fields parameter cannot be set to anything else than all (which is the default). Any other value will break the function. You can modify this very easily to work properly by building in some coditional checks and changing the foreach in the first code block or changing the use of array_search in the second code block

  • You can very easily change the function to move a selected term to the front if needed. You can also adjust the function to accept multiple terms to move

  • For any other info regarding the use of this function, please see get_terms

EDIT

This code in now tested and working as expected.