Reputation: 471
I have a custom hierarchical taxonomy 'Regions' and I create terms on 3 levels: country > state > city. This is being used for SEO purposes, so I have an insane number of cities (60k+).
I added about 12k terms so far and taxonomy admin pages became painfully slow because WP pulls all existing taxonomies into Parent dropdown. Now I'm trying to limit the depth of this dropdown menu to 2 levels - countries and states only. A city will never be a parent of another city, so I'm good to do this.
I was trying to follow https://wordpress.stackexchange.com/questions/106164/how-to-disable-page-attributes-dropdown-in-wp-admin but no luck - I can't figure out how to alter args for wp_dropdown_categories, which (I suppose) is what I need.
I tried this in my functions.php:
add_filter( 'wp_dropdown_categories', 'limit_parents_wpse_106164' );
function limit_parents_wpse_106164( $args )
{
$args['depth'] = '1';
return $args;
}
But this doesn't work, the parent dropdown is still populated with all terms. What am I missing here? Thanks in advance.
Upvotes: 3
Views: 4054
Reputation: 3816
Lets have a look to the code part which generate the parent dropdown:
(wp-admin\edit-tag-form.php)
<?php if ( is_taxonomy_hierarchical($taxonomy) ) : ?>
<tr class="form-field term-parent-wrap">
<th scope="row"><label for="parent"><?php _ex( 'Parent', 'term parent' ); ?></label></th>
<td>
<?php
$dropdown_args = array(
'hide_empty' => 0,
'hide_if_empty' => false,
'taxonomy' => $taxonomy,
'name' => 'parent',
'orderby' => 'name',
'selected' => $tag->parent,
'exclude_tree' => $tag->term_id,
'hierarchical' => true,
'show_option_none' => __( 'None' ),
);
/** This filter is documented in wp-admin/edit-tags.php */
$dropdown_args = apply_filters( 'taxonomy_parent_dropdown_args', $dropdown_args, $taxonomy, 'edit' );
wp_dropdown_categories( $dropdown_args ); ?>
<?php if ( 'category' == $taxonomy ) : ?>
<p class="description"><?php _e('Categories, unlike tags, can have a hierarchy. You might have a Jazz category, and under that have children categories for Bebop and Big Band. Totally optional.'); ?></p>
<?php endif; ?>
</td>
</tr>
<?php endif; // is_taxonomy_hierarchical() ?>
As you can see, they use another filter hook: taxonomy_parent_dropdown_args
So let's try this:
add_filter( 'taxonomy_parent_dropdown_args', 'limit_parents_wpse_106164', 10, 2 );
function limit_parents_wpse_106164( $args, $taxonomy ) {
if ( 'my_custom_taxonomy' != $taxonomy ) return $args; // no change
$args['depth'] = '1';
return $args;
}
Upvotes: 10