How to add a multi-select box meta box in the WordPress user section showing custom post type title.

Multiple select has been added using the following code

  //add meta filed in user in admin
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { ?>
    <h3><?php _e("User Brand", "blank"); ?></h3>

    <table class="form-table">
        <th><label for="user_brand"><?php _e("Brand"); ?></label></th>
                $brand_id=get_user_meta( $user->ID, 'my_meta_box_select');
                <select name="my_meta_box_select[]" id="my_meta_box_select" style="width: 400px;height: 140px;" multiple>
                      <option>Select Brand Here</option>
            $brand_item_arr = [];
            $brand_item_arr['select']='Select Here';
            $term_query = new WP_Term_Query( array( 
            'taxonomy' => 'pa_brand', 
            'hide_empty'             => false,
            ) );

            if ( ! empty( $term_query->terms ) ) 
                foreach ( $term_query ->terms as $term ) 
                        <option value="<?php echo $term->term_id; ?>" <?php if (in_array($term->term_id, $brand_id[0],false)){ ?> selected='selected' <?php  } ?> ><?php echo $term->name; ?></option>

<?php }  
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

function save_extra_user_profile_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) ) { 
        return false; 
    update_user_meta( $user_id, 'my_meta_box_select', $_POST['my_meta_box_select'] );

after that added jquery to make it select2

  • What have you tried? WPSE works a lot better if you include some code showing what you've attempted so someone can guide you as to how to make it work. May 17 at 16:26

Your Answer

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

Browse other questions tagged or ask your own question.