Using WordPress ‘get_terms_args’ PHP filter

The get_terms_args WordPress PHP filter allows you to modify the query arguments used when retrieving terms with get_terms().

Usage

add_filter('get_terms_args', 'your_custom_function', 10, 2);

function your_custom_function($args, $taxonomies) {
    // your custom code here
    return $args;
}

Parameters

  • $args (array): An array of get_terms() arguments.
  • $taxonomies (string[]): An array of taxonomy names.

More information

See WordPress Developer Resources: get_terms_args

Examples

Exclude specific term IDs from the query

This example excludes terms with IDs 10 and 20 from the terms query.

add_filter('get_terms_args', 'exclude_specific_terms', 10, 2);

function exclude_specific_terms($args, $taxonomies) {
    $args['exclude'] = array(10, 20);
    return $args;
}

Order terms by their term IDs

This example orders the terms query by term IDs.

add_filter('get_terms_args', 'order_terms_by_id', 10, 2);

function order_terms_by_id($args, $taxonomies) {
    $args['orderby'] = 'term_id';
    return $args;
}

Display only empty terms

This example retrieves only the terms with no assigned posts.

add_filter('get_terms_args', 'show_empty_terms_only', 10, 2);

function show_empty_terms_only($args, $taxonomies) {
    $args['hide_empty'] = false;
    return $args;
}

Limit the number of terms returned

This example limits the number of terms returned to 5.

add_filter('get_terms_args', 'limit_number_of_terms', 10, 2);

function limit_number_of_terms($args, $taxonomies) {
    $args['number'] = 5;
    return $args;
}

Change the default order of terms to descending

This example changes the default order of terms to descending.

add_filter('get_terms_args', 'change_terms_order', 10, 2);

function change_terms_order($args, $taxonomies) {
    $args['order'] = 'DESC';
    return $args;
}