3

I've created a custom non-hierarchical taxonomy and changed it to display as check boxes instead of a tag cloud. However when I am in the post editor and select on of the available tags then update the post something strange happens. The tag is now saved as the tag ID number and the Tag ID number shows up as a checked checkbox in the Tags metabox.

Here is my code for the Taxonomy:

add_action( 'init', 'create_cuturalmaptags_taxonomy', 0 );

function create_cuturalmaptags_taxonomy() {

  $labels = array(
    'name' => _x( 'Tags', 'taxonomy general name' ),
    'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Tags' ),
    'all_items' => __( 'All Tags' ),
    'parent_item' => __( 'Parent Tag' ),
    'parent_item_colon' => __( 'Parent Tag:' ),
    'edit_item' => __( 'Edit Tag' ), 
    'update_item' => __( 'Update Tag' ),
    'add_new_item' => __( 'Add New Tag' ),
    'new_item_name' => __( 'New Tag Name' ),
    'menu_name' => __( 'Tags' ),
  );    

  register_taxonomy('cuturalmap-tags', array('post'), array(
    'hierarchical' => false,
    'meta_box_cb' => 'post_categories_meta_box',
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'cuturalmap-tags' ),
  ));

}

Here's the example of what's happening. When I go to edit the post and select the tags I want it looks normal. enter image description here

But once I update the post the tags look like this: enter image description here

I confirmed in the tag manager that the number is the ID of the tag I created. Any reason why this is happening and how I can fix it?

Here is my code for the CPT where the 'culturalmap-tags' gets called.

// Cultural Mapping Custom Post Type
function culturalmap_post_type() {

    $labels = array(
        'name'                => _x( 'Cultural Map', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Cultural Map', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Cultural Mapping', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Cultural Map:', 'text_domain' ),
        'all_items'           => __( 'All Cultural Maps', 'text_domain' ),
        'view_item'           => __( 'View Post', 'text_domain' ),
        'add_new_item'        => __( 'Add New Cultural Map', 'text_domain' ),
        'add_new'             => __( 'Add New', 'text_domain' ),
        'edit_item'           => __( 'Edit Post', 'text_domain' ),
        'update_item'         => __( 'Update Cultural Map', 'text_domain' ),
        'search_items'        => __( 'Search Cultural Maps', 'text_domain' ),
        'not_found'           => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
    );
    $args = array(
        'label'               => __( 'Cultural Map', 'text_domain' ),
        'description'         => __( 'Post Type Description', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields' ),
        'taxonomies'          => array( 'cultural-mapping', 'cuturalmap-tags' ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'menu_icon'           => 'dashicons-admin-site',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
        'rewrite'             => array('slug' => 'cultural-map'),

    );
    register_post_type( 'culturalmap', $args );

}

// Hook into the 'init' action
add_action( 'init', 'culturalmap_post_type', 0 );
  • I can't test this right now, but post_categories_meta_box is for hierarchical taxonomies; yours is non-hierarchical. If you change (or remove) that parameter, does that fix the problem? Alternatively, are there any plugins (or theme) you have running that could be modifying metabox functionality? – Tim Malone Jul 24 '16 at 20:51
  • @TimMalone If the tags are in a tag cloud then there is no error it's when I change the metabox display to checkboxes that the errors occur. I need them to show as checkboxes in the editor though and a previous wordpress.stackechange question (wordpress.stackexchange.com/questions/109349/…) mentioned post_categories_meta_box as a solution. I also tried custom code from these instructions (code.tutsplus.com/tutorials/…) changes the radio buttons to checkboxes but I got the same error. – tiaNaturally Jul 24 '16 at 21:35
  • What about setting hierarchical to true? – Tim Malone Jul 24 '16 at 22:08
  • @TimMalone is there a difference for SEO/page functionailty? I already have some hierarchical taxonomies in use and these could potentially have a lot of different options – tiaNaturally Jul 24 '16 at 22:18
  • 1
    SEO you'd have to ask an SEO expert.. but I suggest editing your question because there's obviously quite a lot of conditions behind what you want to do. The simple answer is 'make it hierarchical' or 'use the proper metabox' - I think you need to provide all the conditions for what you want/why you want it. – Tim Malone Jul 24 '16 at 23:07
0

Faced this problem too.

You can solve it adding to functions.php filtering code:

add_action( 'admin_init', function() {
    if( isset( $_POST['tax_input'] ) && is_array( $_POST['tax_input'] ) ) {
        $new_tax_input = array();
        foreach( $_POST['tax_input'] as $tax => $terms) {
            if( is_array( $terms ) ) {
              $taxonomy = get_taxonomy( $tax );
              if( !$taxonomy->hierarchical ) {
                  $terms = array_map( 'intval', array_filter( $terms ) );
              }
            }
            $new_tax_input[$tax] = $terms;
        }
        $_POST['tax_input'] = $new_tax_input;
    }
});

Full code in forked snippet https://gist.github.com/antonlukin/da2b8107c67e677928a87398d89ca202

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.