Talha Talha - 10 days ago 6
PHP Question

Meta box Dropdown menu not save in my custom post type

I have a custom meta-box for certain custom post type, and one of those fields in the meta-box is a drop down like here!
Here is my code

add_action('add_meta_boxes','mind_scholar_meta_box');
function mind_scholar_meta_box()
{
add_meta_box('ms_meta_box','Info Meta Box','mind_scholar_meta_fields','team','normal','high');
}
function mind_scholar_meta_fields()
{

global $post;
$field=get_post_meta($post->ID,'mind_scholar_key',true);
$select=get_post_meta($post->ID,'mind_scholar_select_key',true);
?>
<html>
<body>
<table>
<tr>
<td weidth="50%">
School Name:
</td>
<td weidth="50%">
<input type="text" name="first" value="<?php echo $field ?>">
</td>
</tr>
<tr>
<td width="50%">
Stars Rating:
</td>
<td>
<select>
<option value="star_1" <?php selected( $select, 'star_1' ); ?>>Star 1</option>
<option value="star_2" <?php selected( $select, 'star_2' ); ?>>Star 2</option>
<option value="star_3" <?php selected( $select, 'star_3' ); ?>>Star 3</option>
<option value="star_4" <?php selected( $select, 'star_4' ); ?>>Star 4</option>
<option value="star_5" <?php selected( $select, 'star_5' ); ?>>Star 5</option>
</select>
</td>
</tr>
</table>
</body>
</html>
<?php
}
add_action('save_post','save_posts');
function save_posts()
{
global $post;
$ms_field=$_POST['first'];
$ms_field_select=$_POST[''];

update_post_meta($post->ID,'mind_scholar_key',$ms_field);
update_post_meta($post->ID,'mind_scholar_select_key',$ms_field_select);

}


Unfortunately my drop-down is not saved in my CPT! Can anyone help? :(

Answer

Copy this code and use

add_action('add_meta_boxes','mind_scholar_meta_box'); function mind_scholar_meta_box() {    add_meta_box('ms_meta_box','Info Meta Box','mind_scholar_meta_fields','team','normal','high'); } function mind_scholar_meta_fields() {

   global $post;    $field=get_post_meta($post->ID,'mind_scholar_key',true);    $select=get_post_meta($post->ID,'mind_scholar_select_key',true);    ?> <html>    <body>    <table>
      <tr>
         <td weidth="50%">
            School Name:
         </td>
         <td weidth="50%">
            <input type="text" name="first" value="<?php echo $field ?>">
         </td>
      </tr>
      <tr>
         <td width="50%">
            Stars Rating:
         </td>
         <td>
            <select name='second_select'>
               <option value="star_1" <?php selected( $select, 'star_1' ); ?>>Star 1</option>
               <option value="star_2" <?php selected( $select, 'star_2' ); ?>>Star 2</option>
               <option value="star_3" <?php selected( $select, 'star_3' ); ?>>Star 3</option>
               <option value="star_4" <?php selected( $select, 'star_4' ); ?>>Star 4</option>
               <option value="star_5" <?php selected( $select, 'star_5' ); ?>>Star 5</option>
            </select>
         </td>
      </tr>    </table>    </body>    </html>    <?php } add_action('save_post','save_posts'); function save_posts() {    global $post;    $ms_field=$_POST['first'];    $ms_field_select=$_POST['second_select'];

   update_post_meta($post->ID,'mind_scholar_key',$ms_field);    update_post_meta($post->ID,'mind_scholar_select_key',$ms_field_select);

}
Comments