自定义分类 - 限制父级下拉深度

bug*_*er9 3 wordpress

我有一个自定义的分层分类法“区域”,我在 3 个级别上创建术语:国家 > 州 > 城市。这是用于搜索引擎优化目的,所以我有一个疯狂的城市数量(60k+)。

到目前为止,我添加了大约 12k 个术语,并且分类管理页面变得非常缓慢,因为 WP 将所有现有分类拉入父下拉列表。现在我试图将此下拉菜单的深度限制为 2 个级别 - 仅限国家和州。一个城市永远不会是另一个城市的父级,所以我很高兴这样做。

我试图按照https://wordpress.stackexchange.com/questions/106164/how-to-disable-page-attributes-dropdown-in-wp-admin但没有运气 - 我不知道如何改变 args对于wp_dropdown_categories,(我想)这是我需要的。

我在我的functions.php中试过这个:

add_filter( 'wp_dropdown_categories', 'limit_parents_wpse_106164' );

function limit_parents_wpse_106164( $args )
{
    $args['depth'] = '1';
    return $args;
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用,父下拉列表仍然填充了所有术语。我在这里缺少什么?提前致谢。

And*_*sch 8

让我们看看生成父下拉列表的代码部分:
(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() ?>
Run Code Online (Sandbox Code Playgroud)

如您所见,他们使用了另一个过滤器钩子: taxonomy_parent_dropdown_args

所以让我们试试这个:

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;
}
Run Code Online (Sandbox Code Playgroud)