The get_terms_fields WordPress PHP filter is used to modify the fields selected in the terms query.
Usage
add_filter('get_terms_fields', 'your_custom_function', 10, 3); function your_custom_function($selects, $args, $taxonomies) { // your custom code here return $selects; }
Parameters
- $selects (string[]): An array of fields to select for the terms query.
- $args (array): An array of term query arguments.
- $taxonomies (string[]): An array of taxonomy names.
More information
See WordPress Developer Resources: get_terms_fields
Examples
Add a custom field to the terms query
This code adds a custom field called ‘term_order’ to the terms query.
add_filter('get_terms_fields', 'add_term_order_field', 10, 3); function add_term_order_field($selects, $args, $taxonomies) { $selects[] = 'term_order'; return $selects; }
Remove a field from the terms query
This code removes the ‘description’ field from the terms query.
add_filter('get_terms_fields', 'remove_description_field', 10, 3); function remove_description_field($selects, $args, $taxonomies) { $key = array_search('description', $selects); if ($key !== false) { unset($selects[$key]); } return $selects; }
Modify fields based on taxonomy
This code modifies the fields in the terms query only for the ‘category’ taxonomy.
add_filter('get_terms_fields', 'modify_category_fields', 10, 3); function modify_category_fields($selects, $args, $taxonomies) { if (in_array('category', $taxonomies)) { $selects[] = 'term_order'; } return $selects; }
Modify fields based on term query arguments
This code modifies the fields in the terms query if the ‘hide_empty’ argument is set to true.
add_filter('get_terms_fields', 'modify_fields_based_on_args', 10, 3); function modify_fields_based_on_args($selects, $args, $taxonomies) { if (isset($args['hide_empty']) && $args['hide_empty']) { $selects[] = 'term_order'; } return $selects; }
Replace fields in the terms query
This code replaces the default fields with custom fields in the terms query.
add_filter('get_terms_fields', 'replace_fields', 10, 3); function replace_fields($selects, $args, $taxonomies) { return array('term_id', 'name', 'slug', 'term_order'); }