0

I have a select list in a metabox which value is saved successfully after clicking "Update" when editing post. Of course, the values are custom taxonomies. But i don't know how to add another select list and make it save its values successfully too...

add_action( 'add_meta_boxes', 'so_custom_meta_box' );

function so_custom_meta_box($post){
add_meta_box('so_meta_box', 'Reproductor 1', 'custom_element_grid_class_meta_box',     $post->post_type, 'normal' 

, 'high');
}

add_action('save_post', 'so_save_metabox');

function so_save_metabox(){ 
global $post;

if(isset($_POST["custom_element_grid_class"])){
     //UPDATE: 
    $meta_element_class = $_POST['custom_element_grid_class'];
    //END OF UPDATE

    update_post_meta($post->ID, 'custom_element_grid_class_meta_box',   $meta_element_class);
    //print_r($_POST);
}
}

function custom_element_grid_class_meta_box($post){
$meta_element_class = get_post_meta($post->ID, 'custom_element_grid_class_meta_box', true); ?>   

  <select name="custom_element_grid_class" id="custom_element_grid_class">
  <option value="Netu" <?php selected( $meta_element_class, 'Netu' ); ?>>Netu</option>
  <option value="Netu-latino" <?php selected( $meta_element_class, 'Netu-latino' ); ? >>Netu Latino</option>
  <option value="Netu-castellano" <?php selected( $meta_element_class, 'Netu- castellano' ); ?>>Netu Castellano</option>
  <option value="Netu-vose" <?php selected( $meta_element_class, 'Netu-vose' ); ?>>Netu    Vose</option>
</select>

  <select name="custom_element_grid_class" id="custom_element_grid_class">
  <option value="Vk" <?php selected( $meta_element_class, 'Vk' ); ?>>Vk</option>
  <option value="Vk-latino" <?php selected( $meta_element_class, 'Vk-latino' ); ?>>Vk  Latino</option>
  <option value="Vk-castellano" <?php selected( $meta_element_class, 'Vk-castellano' );  ?>>Vk Castellano</option>
  <option value="Vk-vose" <?php selected( $meta_element_class, 'Vk-vose' ); ?>>Vk    Vose</option>
</select>

<?php
}

Only the first select list values are saved when i click update, but not the second. I already know this has to be with the save code but i can't figure out how to be able to modify it so i can put a second select list and its value can be saved.

1

Well they share a same name... try to declare name as array.

<select name="custom_element_grid_class[]" id="custom_element_grid_class">
  • I am very newbie in this php thing, how to do that? – Rodrigo Jul 15 '14 at 6:58
0

Try something like this:

     add_action( 'add_meta_boxes', 'so_custom_meta_box' );

        function so_custom_meta_box($post){
        add_meta_box('so_meta_box', 'Reproductor 1', 'custom_element_grid_class_meta_box',     $post->post_type, 'normal' 

        , 'high');
        }

        add_action('save_post', 'so_save_metabox');

        function so_save_metabox(){ 
        global $post;

        if(isset($_POST["custom_element_grid_class"]) && isset($_POST["custom_element_grid_class2"])){
             //UPDATE: 
            $meta_element_class = $_POST['custom_element_grid_class'];
            $meta_element_class2 = $_POST['custom_element_grid_class2'];
            //END OF UPDATE

            update_post_meta($post->ID, 'custom_element_grid_class_meta_box',   $meta_element_class);
        update_post_meta($post->ID, 'custom_element_grid_class_meta_box2',   $meta_element_class2);
            //print_r($_POST);
        }
        }

        function custom_element_grid_class_meta_box($post){
        $meta_element_class = get_post_meta($post->ID, 'custom_element_grid_class_meta_box', true); 
$meta_element_class2 = get_post_meta($post->ID, 'custom_element_grid_class_meta_box2', true); ?>  

          <select name="custom_element_grid_class" id="custom_element_grid_class">
          <option value="Netu" <?php selected( $meta_element_class, 'Netu' ); ?>>Netu</option>
          <option value="Netu-latino" <?php selected( $meta_element_class, 'Netu-latino' ); ? >>Netu Latino</option>
          <option value="Netu-castellano" <?php selected( $meta_element_class, 'Netu- castellano' ); ?>>Netu Castellano</option>
          <option value="Netu-vose" <?php selected( $meta_element_class, 'Netu-vose' ); ?>>Netu    Vose</option>
        </select>

          <select name="custom_element_grid_class2" id="custom_element_grid_class">
          <option value="Vk" <?php selected( $meta_element_class2, 'Vk' ); ?>>Vk</option>
          <option value="Vk-latino" <?php selected( $meta_element_class2, 'Vk-latino' ); ?>>Vk  Latino</option>
          <option value="Vk-castellano" <?php selected( $meta_element_class2, 'Vk-castellano' );  ?>>Vk Castellano</option>
          <option value="Vk-vose" <?php selected( $meta_element_class2, 'Vk-vose' ); ?>>Vk    Vose</option>
        </select>

        <?php
        }

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.