• I have created a taxonomy “Ospiti” (guests) and set a selector in order to write the number of guests. If I select the number “7” and then edit the post , I see in the posts admin colum the n “7” correctly in correspondence of that post (apartment), but if I want to edit again that post the selector has lost the selection (I see it on 2 and not on 7). Pic of the selector: https://ibb.co/9wtqcdr

    I write here the code I used, someone can fix this error?

    // Creazione tassonomia ospiti
    add_action( 'init', function() {
        $labels = array(
            'name'              => _x( 'Ospiti', 'taxonomy general name' ),
            'singular_name'     => _x( 'Ospite', 'taxonomy singular name' ),
        );
        register_taxonomy( 'numero_ospiti', array( 'post' ), array(
            'hierarchical'      => false,
            'labels'            => $labels,
    'meta_box_cb'       => "post_categories_meta_box",
    'show_admin_column'  => true,
     'public'            => true,
            
        ) );
    } );
    
    add_action( 'admin_head', function() {
        ?>
        <style type="text/css">
            #newtaxonomy_name_parent {
                display: none;
            }
        </style>
        <?php
    });
    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;
        }
    });
    
    //creating a META BOX
    function custom_meta_box_markup($object)
    {
        wp_nonce_field(basename(__FILE__), "meta-box-nonce");
        ?>
        <div>
          
            <label for="meta-box-ospiti">N.ospiti</label>
            <?php
            $selected_terms = wp_get_post_terms($object->ID, 'numero_ospiti', array('fields' => 'ids'));
            $args = array(
                'taxonomy' => 'numero_ospiti',
                'selected' => $selected_terms,
                'name' => 'tax_input[numero_ospiti][]',
                'hide_empty' => false
            );
            wp_dropdown_categories($args);
            ?>
        </div>
        <?php
    }
    
    function add_custom_meta_box()
    {
         add_meta_box("demo-meta-box", "Ospiti", "custom_meta_box_markup",
    "post", "side", "high", null);
    }
    
    add_action("add_meta_boxes", "add_custom_meta_box");
    

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Hi @sacconi. I think the problem might be that selected needs to be set to a single term (if it exists) instead of an array of terms.

    function custom_meta_box_markup($object)
    {
        wp_nonce_field(basename(__FILE__), "meta-box-nonce");
        ?>
        <div>
          
            <label for="meta-box-ospiti">N.ospiti</label>
            <?php
            $selected_terms = wp_get_post_terms($object->ID, 'numero_ospiti', array('fields' => 'ids'));
            $selected = !empty($selected_terms) ? $selected_terms[0] : ''; // Get the first selected term or an empty string
            $args = array(
                'taxonomy' => 'numero_ospiti',
                'selected' => $selected,
                'name' => 'tax_input[numero_ospiti][]',
                'hide_empty' => false
            );
            wp_dropdown_categories($args);
            ?>
        </div>
        <?php
    }
    

    Let me know if that works.

Viewing 1 replies (of 1 total)
  • The topic ‘how making a selection from a taxonomy selector fixed definitively’ is closed to new replies.