WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom post type where each post can only be seen on the frontend by one specific user.

I have figured out how to make an archive template where only posts with the meta _allowed_user = user_ID are queried.

But now I gotta create a meta box that lists all users (pretty much like the author box) so the publisher can choose who is allowed to see the post...

share|improve this question
up vote 0 down vote accepted

If someone wants to clean this up, all the better, but it gets the job done :)

If I understood you correctly, you need the ID for the user, yes?

For more info on $wp_user_query check out this post which was a partial resource for me writing this code: 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;

    return $post->ID;

  update_post_meta($post->ID, "users", $_POST["users"]);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.