• This is my example code for the meta box.

    foreach ($users as $user) {
      $options[$user->ID] = $user->user_login;
     }
     
     $meta_box->addSelect($prefix . "a[]", $options, array('name' => 'name', 'multiple' => 
     true));

    And here’s the code that should save the selections.

    for ($users as $user) {
       update_user_meta( $user->ID, "a", $_POST["a"]);

    I don’t know why it’s not working. If I switch to single select by setting ‘multiple’ to false and removing the ‘[]’ it works, but it doesn’t work when I’m trying to multi-select.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    update_user_meta() should save an array of data from multiselect. There’s not enough context to fully evaluate your code. Perhaps use your browser’s network developer tool to verify the form data is submitted as your code expects.

    I hope you know that you shouldn’t normally save data to your DB from $_POST without first validating and sanitizing the data. I imagine you’re currently noodling about with your code and intend to add such functionality later. It’s worth mentioning just in case not.

    Thread Starter Joseph Yhu

    (@josephyhu)

    Sorry, I can’t show much for privacy reasons.

    The first code is in a function in meta boxes file, and the second is in a function in a different file. $users = get_users(). I added save post add_action in a different file.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Selecting Multiple Users with Select Doesn’t Get Saved’ is closed to new replies.