title: How to dynamically assign author to post in WordPress?
body: <p>I'm creating a WordPress plugin which has a custom meta-box that lists checkboxes of authors and contributors. When checked, the list shows up on frontend at the end of the post. What I need to do is: When user clicks the name of a contributor, it reverts to archives page, but the post isn't listed under the name of that contributors.</p>
  
  <p>How can I update and save a post under multiple contributors so that it is showed under the author's archives page?</p>
  
  <p>This is the custom meta-box callback function and the function which is called when the post is saved:</p>
  
  <pre><code>function cd_meta_box_cb($post)
  {
      global $post;
      echo'&lt;b&gt; Select the contributors that have contributed to this post: &lt;/b&gt;';
      echo '&lt;br&gt;&lt;br&gt;';
      wp_nonce_field('my_meta_box_nonce', 'meta_box_nonce');
      global $wpdb;
  
      $authors=$wpdb-&gt;get_results("SELECT wp_users.ID, wp_users.user_nicename 
      FROM wp_users INNER JOIN wp_usermeta 
      ON wp_users.ID = wp_usermeta.user_id 
      WHERE wp_usermeta.meta_key = 'wp_capabilities' 
      AND wp_usermeta.meta_value LIKE '%author%' OR wp_usermeta.meta_value LIKE '%editor%'  
      ORDER BY wp_users.user_nicename");
  
      $current_user = wp_get_current_user();
      foreach ($authors as $author) {
          $author_info=get_userdata($author-&gt;ID);
          //$author_role=$author_info-&gt;roles;
          $author_first_name=$author_info-&gt;first_name;
          $author_last_name=$author_info-&gt;last_name;
          if(strcmp($current_user-&gt;user_nicename,$author-&gt;user_nicename)==0)
          {       
              echo"&lt;input type='checkbox' id='my_meta_box_check' name='my_meta_box_check[]'";
              echo"value=";
              the_author_meta('user_nicename', $author-&gt;ID);
              echo" checked disabled&gt;";
  
              echo"&lt;input type='hidden' id='my_meta_box_check' name='my_meta_box_check[]'";
              echo"value=";
              the_author_meta('user_nicename', $author-&gt;ID);
              echo"&gt;";
          }
          else
          {
              echo"&lt;input type='checkbox' id='my_meta_box_check' name='my_meta_box_check[]'";
              echo"value=";
              the_author_meta('user_nicename', $author-&gt;ID);
              echo"&gt;";    
          }
          echo $author_first_name ." ". $author_last_name ." ";
          echo"(";
          echo"&lt;label id='labelid' for='author'&gt;";
          the_author_meta('user_nicename', $author-&gt;ID);
          echo"&lt;/label&gt;";
          echo")";
          echo "&lt;br /&gt;";
      }
  }
  //save custom data when our post is saved
  function save_custom_data($post_id)
  {
      global $post,$wpdb;
  
      $contributor=get_post_meta($post-&gt;ID, 'my_meta_box_check', true);
      if (defined('DOING_AUTOSAVE') &amp;&amp; DOING_AUTOSAVE) return;
      if (!isset($_POST['meta_box_nonce']) || !wp_verify_nonce($_POST['meta_box_nonce'], 'my_meta_box_nonce')) return;
      if (!current_user_can('edit_post')) return;
      if (isset($_POST['my_meta_box_check'])) 
      {
          update_post_meta($post_id, 'my_meta_box_check', $_POST['my_meta_box_check']);
          $tablename = $wpdb-&gt;prefix.'authorlist';
          $wpdb-&gt;insert($tablename,array('authorname'=&gt;$post_id,'authorpost'=&gt;$contributor));
      }
      else 
      {
          delete_post_meta($post_id, 'my_meta_box_check');
      }
  }
  
  add_action('save_post', 'save_custom_data');
  </code></pre>