I have a post_type named room_type and a taxonomy room_facility. Between them  I want a relationship. Furthermore I would like to add a value for each relationship room_type and room_facility. So the frontend looks like this:

[![enter image description here][1]][1]

The value is stored in postmeta with a meta_key _room_facility_key_<ID>, where <ID> is the id from room_facility and a post_id from room_type. I think this is the easiest way to link the value to room_type and room_facility, right? 
[![enter image description here][2]][2]

You can see I get the values stored in database. The problem is to save them. Here is my code.

To add the metabox:

    function room_facility_custom_meta() {
        add_meta_box( 'room_facility_meta', __( 'Meta Box ', 'room_facility-textdomain' ), 'room_facility_meta_callback', 'room_type' );
    }
    add_action( 'add_meta_boxes', 'room_facility_custom_meta' );

My callback function

    function room_facility_meta_callback( $post ) {
        //nonce_field
        wp_nonce_field( basename( __FILE__ ), 'room_facility_nonce' );
        
        $tax = "room_facility";
        $term_facilities = get_terms( $tax, array('hide_empty' => false));
        $term_checked_facilities= wp_get_post_terms($post->ID, $tax);
    
        foreach($term_facilities as $term){
            
            $room_facility_id = $term->term_id;
            /* the meta_key for postmeta, note there is the room_facility included, 
             * since there should be a relationship between post_id (in this case from taxonomy "room_type") 
             * and "room_facility" 
             */
            $meta_key = "_room_facility_key_".$room_facility_id;
            //value from the postmeta
            $room_facility_value = get_post_meta($post->ID, $meta_key);
    
        ?>
            <div class="row">
                    <label for="checkbox_<?php echo $room_facility_id ?>">
                        <input type="checkbox" name="checkbox_<?php echo $room_facility_id ?>" id="checkbox_<?php echo $room_facility_id ?>"  <?php checked( myArrayContainsWord($term_checked_facilities, $term->term_id)); ?> />
                        <?php _e( $term->name, 'prfx-textdomain' )?>
                    </label>
                    <input type="text" name="text_<?php echo $room_facility_id ?>" id="text_<?php echo $room_facility_id ?>" value="<?php if ( !empty ( $room_facility_value[0] ) ){ echo $room_facility_value[0]; }?>" />
            </div>
        <?php
        } //end foreach
    }

My save function

        function room_facility_meta_save( $post_id ) 
        {
              // Verify this came from the our screen and with proper authorization,
              // because save_post can be triggered at other times
              if ( !wp_verify_nonce( $_POST['room_facility_nonce'], plugin_basename(__FILE__) )) {
                return $post_id;
              }
            
               // Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
              // to do anything
              if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
                return $post_id;
        
              
              //wp_set_object_terms( 742, array(211), 'room_facility');
              $taxonomy = "room_facility";
              $term_facilities = get_terms( $taxonomy, array('hide_empty' => false));
              
              foreach ($term_facilities as $tax => $term)
              {
                $room_facility_id = $term->term_id;
                
                /* the meta_key for postmeta, note there is the room_facility included, 
                 * since there should be a relationship between post_id (in this case from taxonomy "room_type") 
                 * and "room_facility" 
                 */
                $meta_key = "room_facility_key_".$room_facility_id;
                
                //update postmeta room_facility_key_<ID>  
                if( isset( $_POST[ "text_".$room_facility_id] ) ) {
                    update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ "text_".$room_facility_id] ) );
                }
                
                //set wp_term_relationships for room_type and room_facility
                if( isset( $_POST[ 'checkbox_'.$room_facility_id ] ) ) 
                {
                    wp_set_post_terms( $post_id, $term, $taxonomy);
                } /*else {
                    wp_set_post_terms( $post_id, $term, $taxonomy);
                }*/
              }
    }
    add_action( 'save_post', 'room_facility_meta_save' );

I'm not sure if the save function is called, because when I do a 

    echo '<script>alert("test")</script>';
Nothing happens...

I know I posted a lot of code but I really don't Have an idea why its not working. Feel free to ask if you have a question.


  [1]: https://i.sstatic.net/5ESQN.png
  [2]: https://i.sstatic.net/PZ2Nd.png