get_terms_orderbyfilter-hookWP 2.8.0

Filters the part of the SQL query responsible for the sorting column in get_terms(), get_categories(), wp_list_categories().

Usage

function filter_function_name_11( $orderby, $args, $taxonomies ) {
	// Filter...

	return $orderby;
}
add_filter( 'get_terms_orderby', 'filter_function_name_11' );

Parameters

$orderby(string)
When sorting by the name column, this query part looks like t.name or t.slug.
$args(array)
An array of arguments passed to get_terms().
$taxonomies(string/array)
The taxonomy names passed to get_terms().

Examples

#1 Sort categories

Suppose each category slug begins with a number for sorting. If orderby=slug is specified in get_terms(), the result is ordered 1, 10, 2, 3..., but it should be 1, 2, 3...9, 10. Change part of the SQL query to fix this:

// Set the filter
add_filter('get_terms_orderby', 'sort_terms_clause', 10, 3);
function sort_terms_clause( $orderby, $args, $taxonomies ){
	return 't.slug+0';
}

// Get categories
$terms = get_terms('category', array('hide_empty=0') );
foreach( $terms as $term ){
	// Output categories in the required order
}

// Remove the filter
remove_filter('get_terms_orderby', 'sort_terms_clause', 10);

Changelog

Since 2.8.0 Introduced.

Where the hook is called

WP_Term_Query::parse_orderby()
get_terms_orderby
wp-includes/class-wp-term-query.php 956
$orderby = apply_filters( 'get_terms_orderby', $orderby, $this->query_vars, $this->query_vars['taxonomy'] );

Where the hook is used in WordPress

Usage not found.