I have created a dropdown metabox to display all users for my custom post type “news”. Now i need to update the selected value into wp_postmeta table. I have done the same in Wp_options table ,but am failing to do the same in wp_postmeta. Here s my code please help me

function my_meta()
{
  add_meta_box(
       'my_custom_meta',
       'Created By',
       'my_meta_box_mes',
       'news',
       'normal',
       'low'
    );
}
add_action('add_meta_boxes','my_meta');

function my_meta_box_mes()

{
 /*if($_POST['my_custom_meta']){
    update_option( 'username', $_POST['my_custom_meta']);
  }
*/
?>

<select name="my_custom_meta" id="my_custom_meta">

<?php
$result=get_users();

foreach ($result as $row){
  $username=$row->user_login;
?>

<option value = "<?php echo $row->user_login;?>" <?php echo ($row->user_login==get_option('username'))?"selected
":"";?>>
<?php echo $username?>
</option>

<?php
}
?>
</select>
<?php
}
function save_user_option( $post_id ) {
  // var_dump($_POST);exit();
  if($_POST['my_custom_meta']){

    $args=array('ID'=>$postid,
                'user_login'=>$username,
                'meta_key'=>'created_by',
                'meta_value'=>$_POST['my_custom_meta']
      );

    $res=get_post_meta($args);

    update_post_meta($postid,'created_by', $_POST['my_custom_meta']);
  }

}
add_action( 'update_post_meta', 'save_user_option' );
?>

Read more here: How to Update post metadata table with Values from dropdown list


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: