The get_categories_taxonomy WordPress PHP filter allows you to modify the taxonomy used when retrieving terms with the get_categories()
function.
Usage
add_filter('get_categories_taxonomy', 'my_custom_taxonomy', 10, 2); function my_custom_taxonomy($taxonomy, $args) { // your custom code here return $taxonomy; }
Parameters
- $taxonomy (string) – The taxonomy to retrieve terms from.
- $args (array) – An array of arguments. See
get_terms()
for additional arguments.
More information
See WordPress Developer Resources: get_categories_taxonomy
Examples
Change taxonomy to ‘portfolio_categories’
Change the taxonomy used by get_categories()
to ‘portfolio_categories’:
add_filter('get_categories_taxonomy', 'change_to_portfolio_categories', 10, 2); function change_to_portfolio_categories($taxonomy, $args) { return 'portfolio_categories'; }
Conditionally modify taxonomy
Modify the taxonomy based on a specific condition:
add_filter('get_categories_taxonomy', 'conditionally_modify_taxonomy', 10, 2); function conditionally_modify_taxonomy($taxonomy, $args) { if (is_page('portfolio')) { return 'portfolio_categories'; } return $taxonomy; }
Modify taxonomy based on post type
Change the taxonomy based on the post type being queried:
add_filter('get_categories_taxonomy', 'modify_taxonomy_based_on_post_type', 10, 2); function modify_taxonomy_based_on_post_type($taxonomy, $args) { if ($args['post_type'] == 'portfolio') { return 'portfolio_categories'; } return $taxonomy; }
Use custom function to determine taxonomy
Use a custom function to determine the taxonomy to be used:
add_filter('get_categories_taxonomy', 'use_custom_function_for_taxonomy', 10, 2); function use_custom_function_for_taxonomy($taxonomy, $args) { return my_custom_taxonomy_function($args); }
Modify taxonomy based on a custom field
Change the taxonomy based on a custom field value:
add_filter('get_categories_taxonomy', 'modify_taxonomy_based_on_custom_field', 10, 2); function modify_taxonomy_based_on_custom_field($taxonomy, $args) { $custom_field_value = get_post_meta(get_the_ID(), 'custom_field', true); if ($custom_field_value == 'portfolio') { return 'portfolio_categories'; } return $taxonomy; }