如果有人想清理这一切,那就更好了,但它完成了任务:)
如果我理解正确,您需要用户的ID,是吗?
有关的详细信息$wp_user_query
查看这篇文章,它是我编写此代码的部分资源:http://www.mattvarone.com/wordpress/list-users-with-wp_user_query/
// List Users
add_action("admin_init", "users_meta_init");
function users_meta_init(){
add_meta_box("users-meta", "User Select", "users", "post", "normal", "high");
}
function users(){
global $post;
$custom = get_post_custom($post->ID);
$users = $custom["users"][0];
// prepare arguments
$user_args = array(
// search only for Authors role
\'role\' => \'Author\',
// order results by display_name
\'orderby\' => \'display_name\'
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query($user_args);
// Get the results
$authors = $wp_user_query->get_results();
// Check for results
if (!empty($authors))
{
// Name is your custom field key
echo "<select name=\'users\'>";
// loop trough each author
foreach ($authors as $author)
{
// get all the user\'s data
$author_info = get_userdata($author->ID);
$author_id = get_post_meta($post->ID, \'users\', true);
if($author_id == $author_info->ID) { $author_selected = \'selected="selected"\'; } else { $author_selected = \'\'; }
echo \'<option value=\'.$author_info->ID.\' \'.$author_selected.\'>\'.$author_info->first_name.\' \'.$author_info->last_name.\'</option>\';
}
echo "</select>";
} else {
echo \'No authors found\';
}
}
// Save Meta Details
add_action(\'save_post\', \'save_userlist\');
function save_userlist(){
global $post;
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return $post->ID;
}
update_post_meta($post->ID, "users", $_POST["users"]);
}