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, return the result from get_terms()
or type cast the result as an array, remove the desiredthat key/value pair from the array, movepair and add it to the back of that array and then type cast that array back as an object and return the value.
Now, lets put that into code: (I have commented the code to make it easy to follow)
function get_term_to_end( $taxonomy = '', $args = [] )
{
$terms = get_terms( $taxonomy, $args );
/*
* Return get_terms as is$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 get_terms( $taxonomy, $args );$terms;
/*
* If term_to_end is set and has a value, continue to process
*/
$terms = get_terms( $taxonomy, $args );
/*
* 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;
}
function get_term_to_end( $taxonomy = '', $args = [] )
{
$terms = get_terms( $taxonomy, $args );
/*
* Return get_terms as is$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 get_terms( $taxonomy, $args );$terms;
/*
* If term_to_end is set and has a value, continue to process
*/
$terms = get_terms( $taxonomy, $args );
/*
* 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
*
* UseWe 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 posistionposition of the term_to_end
*/
$terms_array = json_decode(array json_encode( $terms ), $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_array as an object to keep uniformity$terms
*/
if ( !$end_term_position )
return (object) $terms_array;$terms;
/*
* Get the key value pair for term_to_end, unset it from $terms_array$terms and reset term_to_end pair at the back
*/
$end_term_pair$end_term_pair[] = $terms_array[$end_term_position];$terms[$end_term_position];
unset( $terms_array[$end_term_position]$terms[$end_term_position] );
$new_terms_array = array_merge( $terms_array(array) $terms, $end_term_pair );
/*
* We are done!! Now we can just return $new_terms_array as an object
*/
return (object) $new_terms_array;
}
EDIT
This code in now tested and working as expected.