• I make a hierarchical custom taxonomy. And How to let Hierarchical taxonomy display like Non-Hierarchical taxonomy?

    https://d7j863fr5jhrr.cloudfront.net/wp-content/uploads/2011/10/categories-264x300.jpg
    Pic1

    Let Pic1 like Pic2 and reserve hierarchical attribute.

    https://d7j863fr5jhrr.cloudfront.net/wp-content/uploads/2011/10/tags-300x136.jpg
    Pic2

    • This topic was modified 7 years, 6 months ago by johnhsu.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Hook the “add_meta_boxes_{$post_type}” action. There’s another one for all post types, but this dynamic one is usually preferable.

    Use remove_meta_box() to remove the one WP assigns for your taxonomy. Examine the values in the global array $wp_meta_boxes to determine the ID for your taxonomy. It should be something like “{$your-taxonomy}div”. Add another meta box using the ID form “tagsdiv-{$your-taxonomy}”. In the arguments for add_meta_box(), include “post_tags_meta_box” as the callback, null for $screen, ‘side’ for $context, ‘core’ for $priority, and in the $callback_args array include 'taxonomy'=> $your-taxonomy, as an element.

    This replaces the default with the one used for non-hierarchical taxonomies. This concept is untested, but it ought to be valid.

    Thread Starter johnhsu

    (@johnhsu)

    I add code like this, it could display in the post edit screen.
    But when i add new tag in it, the post couldn’t save the tag. How to fix it?
    Thanks for help~~~

    function myplugin_add_custom_box() {
    
    add_meta_box('tagsdiv-depart','depart','post_tags_meta_box','post','side','core',array( 'taxonomy' => 'depart'));
    
    }
    
    add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );
    Moderator bcworkz

    (@bcworkz)

    Well, poo! I had thought the standard WP post update would handle this automatically, but I now see the update is handled differently with hierarchical taxonomies and the “flat” style meta box provides data in the wrong form. So automatic handling is out, that does not mean we cannot handle these ourselves.

    A comma separated list of term names ought to be in $_POST['tax_input'][$your-taxonomy] Hook an action like “save_post”. Depending on your situation, there may be better ones, but save_post will certainly work. In your callback, first ensure the data is available. Unslash and sanitize the list in $_POST, then use wp_set_post_terms() to save the data.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to let Hierarchical taxonomy display like Non-Hierarchical taxonomy?’ is closed to new replies.