Why does my Taxonomy in Wordpress not work on a hierarchical level?

Asked

Viewed 493 times

1

My problem is the following, record a custom post type and a custom taxonomy for the same, both with hierarchy enabled. Record custom type 'categories' within taxonomy, following the style

  • Father
    • Son
    • Son Two

But when saving a custom type post within one of the two child categories, the following happens:

Fica desta forma.

That is, the custom type post is registered within the selected taxonomy, but it ceases to follow the hierarchical level.

Below is the creation code for custom type post 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');

1 answer

2


This is an old "problem", but considered as expected behaviour, as a core contributor *:

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

Another builder of Wordpress source code has created a plugin to eliminate this "functionality":

Category Checklist Tree

On the post Editing screen, after saving a post, you will notice that the checked Categories are displayed on 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 very 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 from the list to the first category marked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.