Skip to main content
Post Undeleted by Pieter Goosen
deleted 68 characters in body
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

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.

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 desired key/value pair from the array, move 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

function get_term_to_end( $taxonomy = '', $args = [] )
{
    /*
     * Return get_terms as is 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 );

    /*
     * 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 = [] )
{
    /*
     * Return get_terms as is 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 );

    /*
     * 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
      *
      * Use array_search to and array_column to determine the posistion of the term_to_end
      */
    $terms_array = (array) $terms;
    $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
     */
    if ( !$end_term_position )
        return (object) $terms_array;
    
    /*
     * Get the key value pair for term_to_end, unset it from $terms_array and reset term_to_end pair at the back
     */
    $end_term_pair = $terms_array[$end_term_position];
    unset( $terms_array[$end_term_position] );
    $new_terms_array = array_merge( $terms_array, $end_term_pair );

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

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)

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;
}
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;
}

EDIT

This code in now tested and working as expected.

Post Deleted by Pieter Goosen
Rollback to Revision 1
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

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, and throughcheck if your unique key is set as a parameter and then according to that, return the result from foreachget_terms() loopor type cast the result as an array, remove the desired key/value pair and stick thatfrom the array, move it to the endback of thethat array and then type cast that array back as an object and return the value.

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

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 = [] )
{
    /*
     * Return get_terms as is 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 );

    /*
     * 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
      *
      * Use array_search to and array_column to determine the posistion of the term_to_end
      */
    $terms_array = (array) $terms;
    $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
     */
    if ( !$end_term_position )
        return (object) $terms_array;
    
    /*
     * Get the key value pair for term_to_end, unset it from $terms_array and reset term_to_end pair at the back
     */
    $end_term_pair = $terms_array[$end_term_position];
    unset( $terms_array[$end_term_position] );
    $new_terms_array = array_merge( $terms_array, $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:

  • 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 conditionalcoditional 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. The PHP5.5 version was removed as it has an issue with array_search

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, and through a foreach loop, remove the desired key/value pair and stick that to the end of the array.

This needs at least PHP 5.4

Example use case:

  • 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 conditional checks and changing the foreach in the first 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. The PHP5.5 version was removed as it has an issue with array_search

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 desired key/value pair from the array, move it to the back of that array and then type cast that array back as an object and return the value.

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

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 = [] )
{
    /*
     * Return get_terms as is 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 );

    /*
     * 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
      *
      * Use array_search to and array_column to determine the posistion of the term_to_end
      */
    $terms_array = (array) $terms;
    $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
     */
    if ( !$end_term_position )
        return (object) $terms_array;
    
    /*
     * Get the key value pair for term_to_end, unset it from $terms_array and reset term_to_end pair at the back
     */
    $end_term_pair = $terms_array[$end_term_position];
    unset( $terms_array[$end_term_position] );
    $new_terms_array = array_merge( $terms_array, $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:

  • 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

deleted 2057 characters in body
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

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 fromthrough a get_terms()foreach or type cast the result as an arrayloop, remove the desired key/value pair from the array, move itand stick that to the backend of that array and then type cast that array back as an object and return the valuearray.

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

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 = [] )
{
    /*
     * Return get_terms as is 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 );

    /*
     * 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
      *
      * Use array_search to and array_column to determine the posistion of the term_to_end
      */
    $terms_array = (array) $terms;
    $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
     */
    if ( !$end_term_position )
        return (object) $terms_array;
    
    /*
     * Get the key value pair for term_to_end, unset it from $terms_array and reset term_to_end pair at the back
     */
    $end_term_pair = $terms_array[$end_term_position];
    unset( $terms_array[$end_term_position] );
    $new_terms_array = array_merge( $terms_array, $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:

  • 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 coditionalconditional 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. The PHP5.5 version was removed as it has an issue with array_search

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 desired key/value pair from the array, move it to the back of that array and then type cast that array back as an object and return the value.

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

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 = [] )
{
    /*
     * Return get_terms as is 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 );

    /*
     * 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
      *
      * Use array_search to and array_column to determine the posistion of the term_to_end
      */
    $terms_array = (array) $terms;
    $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
     */
    if ( !$end_term_position )
        return (object) $terms_array;
    
    /*
     * Get the key value pair for term_to_end, unset it from $terms_array and reset term_to_end pair at the back
     */
    $end_term_pair = $terms_array[$end_term_position];
    unset( $terms_array[$end_term_position] );
    $new_terms_array = array_merge( $terms_array, $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:

  • 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

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, and through a foreach loop, remove the desired key/value pair and stick that to the end of the array.

This needs at least PHP 5.4

Example use case:

  • 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 conditional checks and changing the foreach in the first 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. The PHP5.5 version was removed as it has an issue with array_search

Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209
Loading