Using WordPress ‘get_{$taxonomy}’ PHP filter

The get_{$taxonomy} WordPress PHP filter allows you to modify a taxonomy term object. The dynamic part of the hook name, $taxonomy, refers to the slug of the term’s taxonomy. Possible hook names include get_category and get_post_tag.

Usage

add_filter( 'get_{$taxonomy}', 'your_custom_function', 10, 2 );

function your_custom_function( $_term, $taxonomy ) {
    // your custom code here
    return $_term;
}

Parameters

  • $_term (WP_Term): The term object you want to filter.
  • $taxonomy (string): The taxonomy slug associated with the term.

More information

See WordPress Developer Resources: get_{$taxonomy}

Examples

Add a custom property to a category term

This example adds a custom property my_custom_property to the category term object.

add_filter( 'get_category', 'add_custom_property_to_category', 10, 2 );

function add_custom_property_to_category( $_term, $taxonomy ) {
    $_term->my_custom_property = 'Custom Value';
    return $_term;
}

Modify the term name

This example modifies the term name by appending the term count to it.

add_filter( 'get_post_tag', 'modify_term_name', 10, 2 );

function modify_term_name( $_term, $taxonomy ) {
    $_term->name .= ' (' . $_term->count . ')';
    return $_term;
}

Exclude a specific term

This example removes a specific term with the slug ‘exclude-me’ from the list of terms.

add_filter( 'get_category', 'exclude_specific_term', 10, 2 );

function exclude_specific_term( $_term, $taxonomy ) {
    if ( $_term->slug === 'exclude-me' ) {
        return false;
    }
    return $_term;
}

Add a custom CSS class to term

This example adds a custom CSS class to the term based on the term’s ID.

add_filter( 'get_post_tag', 'add_custom_css_class_to_term', 10, 2 );

function add_custom_css_class_to_term( $_term, $taxonomy ) {
    $_term->custom_css_class = 'term-' . $_term->term_id;
    return $_term;
}

This example modifies the term link by appending a custom query parameter to it.

add_filter( 'get_category', 'modify_term_link', 10, 2 );

function modify_term_link( $_term, $taxonomy ) {
    $_term->link .= '?custom_param=value';
    return $_term;
}