Jan 24 2007

Multiple authors per post in WordPress

Over on an academic journal site I’ve been setting up, I needed a way to assign multiple authors for a single post. Tempus Fugit developed a plugin to do just that, except it makes the assumption that anyone who edits a post should be listed as an author—true for blogs, not true for a site with editors posting others’ work. Here’s a simple fix, though, for anyone using Tempus Fugit’s multiple authors plugin…

In the plugin’s php file, delete the function that equates a post editor as an author:

function txfx_oa_add_author($post_ID) {
global $wpdb, $userdata;

if ( !$userdata ) get_currentuserinfo(); // shouldn't be needed, but can't hurt

$main_author = $wpdb->get_var("SELECT `post_author` FROM $wpdb->posts WHERE `ID` = '$post_ID'");

if ( $userdata->ID == $main_author ) return $post_ID; // the person who created the post is editing it... do nothing

// if we're still here, it must be a new author, so see if they are already logged as an author on this post
if ( $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = 'other_author' AND post_id = '$post_ID' AND meta_value = '$userdata->user_login'") ) return $post_ID;

// if we're STILL here, it's time to add this author
add_post_meta($post_ID, 'other_author', $userdata->user_login);

//and we're done
return $post_ID;

}

And at the very bottom of the file, to keep the file from breaking, delete the commands to add that now-ignored editor as an author:

add_action('edit_post', 'txfx_oa_add_author');
add_action('save_post', 'txfx_oa_add_author');

Done and done! Adding an author is manual now—you just add a custom field called “other_author” and type in the second (or third or fourth) author’s name in the “Value” box. And so long as it matches the name of a registered user, that person will be duly added as an author to the post.