Using WordPress ‘get_term’ PHP filter

The get_term WordPress PHP filter allows you to modify a taxonomy term object before it is returned by the get_term() function.

Usage

add_filter('get_term', 'your_custom_function', 10, 2);
function your_custom_function($_term, $taxonomy) {
  // your custom code here
  return $_term;
}

Parameters

  • $_term (WP_Term): The term object to be filtered.
  • $taxonomy (string): The taxonomy slug of the term.

More information

See WordPress Developer Resources: get_term

Examples

Change the term name

Update the term name by appending ‘ (Modified)’ to the original name.

add_filter('get_term', 'change_term_name', 10, 2);
function change_term_name($_term, $taxonomy) {
  $_term->name .= ' (Modified)';
  return $_term;
}

Add custom data to term object

Add custom data to the term object before it is returned.

add_filter('get_term', 'add_custom_data_to_term', 10, 2);
function add_custom_data_to_term($_term, $taxonomy) {
  $_term->custom_data = 'Your custom data';
  return $_term;
}

Remove term description

Remove the term description for a specific taxonomy.

add_filter('get_term', 'remove_term_description', 10, 2);
function remove_term_description($_term, $taxonomy) {
  if ($taxonomy == 'your_taxonomy_slug') {
    $_term->description = '';
  }
  return $_term;
}

Change term slug

Change the term slug by replacing hyphens with underscores.

add_filter('get_term', 'change_term_slug', 10, 2);
function change_term_slug($_term, $taxonomy) {
  $_term->slug = str_replace('-', '_', $_term->slug);
  return $_term;
}

Use term ID as the term name

Replace the term name with its term ID.

add_filter('get_term', 'use_term_id_as_name', 10, 2);
function use_term_id_as_name($_term, $taxonomy) {
  $_term->name = $_term->term_id;
  return $_term;
}