Using WordPress ‘register_taxonomy_args’ PHP filter

The 'register_taxonomy_args' filter allows you to modify the arguments for registering a taxonomy in WordPress.

Usage

To use this filter, you need to add a custom function to your theme’s functions.php file or a custom plugin. Then hook this function to the register_taxonomy_args filter.

Code Example

function custom_register_taxonomy_args( $args, $taxonomy, $object_type ) {
    // Modify $args here
    return $args;
}
add_filter( 'register_taxonomy_args', 'custom_register_taxonomy_args', 10, 3 );

Parameters

  • $args (array): Array of arguments for registering a taxonomy. See the register_taxonomy() function for accepted arguments.
  • $taxonomy (string): Taxonomy key.
  • $object_type (string[]): Array of names of object types for the taxonomy.

Examples

Modify the default slug

Scenario: You want to change the default slug for a custom taxonomy.

function modify_taxonomy_slug( $args, $taxonomy, $object_type ) {
    if ( 'my_custom_taxonomy' === $taxonomy ) {
        $args['rewrite'] = array( 'slug' => 'custom-slug' );
    }
    return $args;
}
add_filter( 'register_taxonomy_args', 'modify_taxonomy_slug', 10, 3 );

This code will change the default slug for the my_custom_taxonomy taxonomy to custom-slug.

Disable the taxonomy’s archive page

Scenario: You want to disable the archive page for a custom taxonomy.

function disable_taxonomy_archive( $args, $taxonomy, $object_type ) {
    if ( 'my_custom_taxonomy' === $taxonomy ) {
        $args['public'] = false;
    }
    return $args;
}
add_filter( 'register_taxonomy_args', 'disable_taxonomy_archive', 10, 3 );

This code will disable the archive page for the my_custom_taxonomy taxonomy.

Make a taxonomy hierarchical

Scenario: You want to make a custom taxonomy hierarchical, like categories.

function make_taxonomy_hierarchical( $args, $taxonomy, $object_type ) {
    if ( 'my_custom_taxonomy' === $taxonomy ) {
        $args['hierarchical'] = true;
    }
    return $args;
}
add_filter( 'register_taxonomy_args', 'make_taxonomy_hierarchical', 10, 3 );

This code will make the my_custom_taxonomy taxonomy hierarchical.

Change the default sorting order

Scenario: You want to change the default sorting order of a custom taxonomy.

function change_taxonomy_sort_order( $args, $taxonomy, $object_type ) {
    if ( 'my_custom_taxonomy' === $taxonomy ) {
        $args['sort'] = true;
        $args['args'] = array( 'orderby' => 'term_order' );
    }
    return $args;
}
add_filter( 'register_taxonomy_args', 'change_taxonomy_sort_order', 10, 3 );

This code will change the default sorting order of the my_custom_taxonomy taxonomy based on the term_order.

Remove taxonomy from REST API

function remove_taxonomy_from_rest( $args, $taxonomy, $object_type ) {
if ( 'my_custom_taxonomy' === $taxonomy ) { 
$args['show_in_rest'] = false; 
} 
return $args; 
} 
add_filter( 'register_taxonomy_args', 'remove_taxonomy_from_rest', 10, 3 );

Scenario: You want to remove a custom taxonomy from the REST API.

This code will remove the `my_custom_taxonomy` taxonomy from the REST API, making it inaccessible through the REST API endpoints.