Why does my Taxonomy in Wordpress not work hierarchically?

1

My problem is as follows, register a custom post type and a custom taxonomy for it, both with hierarchy enabled. Record the 'categories' of the custom type within the taxonomy, following the style

  • Father
    • Son
    • Son Two

But when you save a custom type post into one of two child categories, the following happens:

That is, the custom type post is registered within the selected taxonomy, but it no longer follows the hierarchical level.

Below is the custom type post creation code and taxonomy.

     $label = array(
        'name' => _x('Arquivos', 'post type general name'),
        'singular_name' => _x('Arquivo', 'post type singular name'),
        'add_new' => _x('Adicionar Arquivo', 'event'),
        'add_new_item' => __('Adicionar novo Arquivo'),
        'edit_item' => __('Editar Arquivo'),
        'new_item' => __('Novo Arquivo'),
        'view_item' => __('Ver Arquivo'),
        'search_items' => __('Procurar Arquivo'),
        'not_found' => __('Arquivo não encontrado'),
        'not_found_in_trash' => __('Arquivo não encontrado na lixeira')
    );

    $args = array(
        'labels' => $label,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'menu_icon' => 'dashicons-upload',
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'menu_position' => null,
        'supports' => array('title', 'editor'),
        'taxonomies' => Array('categorias_arquivos')
    );

    register_post_type('arquivos', $args);

    register_taxonomy('categorias_arquivos', array('arquivos'), array(
        'hierarchical' => true,
        'label' => 'Categorias',
        'singular_label' => 'Categoria',
        'rewrite' => false)
    );

    register_taxonomy_for_object_type('categorias_arquivos', 'arquivos');
    
asked by anonymous 24.11.2014 / 20:10

1 answer

2

This is an old "problem", but considered as expected behavior , as one core contributor * :

  

* (Core Trac ticket - Bug)
The behavior is strange, but it corresponds to how categories appear after saving a post. All selected categories are shown at the top of the category list, regardless of their hierarchy.

Another WordPress source code builder has created a plugin to eliminate this "feature":

  

Category Checklist Tree

     

On the post editing screen, after saving a post, you will notice that the categories are displayed on the top, breaking the category hierarchy. This plugin removes that "feature".
Additionally, it automatically scrolls to the first checked category. Works with custom taxonomies too.

The plugin is pretty simple and the basic code is:

// Função anônima, requer PHP 5.3+
add_filter( 'wp_terms_checklist_args', function ( $args ) {
    $args['checked_ontop'] = false;
    return $args;
});

The jQuery part in the plugin is to scroll the list up to the first checked category.

    
26.11.2014 / 15:30