Search code examples
wordpresswordpress-themingcustom-wordpress-pageswordpress-gutenberg

Changing taxonomy "hierarchical" setting with hook


I am using a free Real Estate Wordpress plugin from the repository but the support is paid.

Anyway, the plugin's "City" taxonomy is NOT hierarchical. I need to make it HIERARCHICAL so I can create counties with hierarchical cities under it. As you know, modifying plugin's files is not a possibility for known reasons (update overwriting).

I am looking for something like this to deploy in functions.php:

function change_post_object_label( $taxonomy_args ) {
    $taxonomy_args->hierarchical = true;
}
add_action( 'taxonomy_hook', 'change_taxonomy_args' );

Does it exist? How can I set hierarchical to "true" for a given taxonomy without having to alter the php files?


Solution

  • If you are working on WordPress 4.4 or a newer version, you may use the register_taxonomy_args filter.

    Add this to your functions.php, and don't forget to use the actual taxonomy slug.

    function filter_register_taxonomy_args( $args, $taxonomy ) {
    
        if ( $taxonomy === 'taxonomy_slug_here' ) {
    
            $args['hierarchical'] = true;
        }
        return $args;
    }
    add_filter( 'register_taxonomy_args', 'filter_register_taxonomy_args', 10, 2 );