Skip to content

Instantly share code, notes, and snippets.

@michaeldozark
Created September 12, 2014 19:35
Show Gist options
  • Save michaeldozark/12ca766df0f5d6de29ef to your computer and use it in GitHub Desktop.
Save michaeldozark/12ca766df0f5d6de29ef to your computer and use it in GitHub Desktop.
Order terms by name cast as a number. Originally used for retrieving WooCommerce attributes. To use, set the 'orderby' argument in your `get_terms` call to 'name_as_number'.
add_filter( 'get_terms_orderby', 'slimline_get_terms_orderby_name_as_number', 10, 2 );
/**
* slimline_get_terms_orderby_name_as_number function
*
* @param string $orderby The orderby string created from the initial arguments passed through `get_terms`
* @param array $args `get_terms` arguments
* @return string $orderby Name cast as a number if `orderby` is 'name_as_number', otherwise unchanged
*/
function slimline_get_terms_orderby_name_as_number( $orderby, $args ) {
if ( 'name_as_number' === $args[ 'orderby' ] ) {
$orderby = 't.name+0';
} // if ( 'name_as_number' === $args[ 'orderby' ] )
return $orderby;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment