Skip to main content
2 of 3
Added details on where to put the code.
brasofilo
  • 22.1k
  • 8
  • 69
  • 264

I've answered a similar Question and the same code can be adapted for this case. This creates a Meta Box where you can assign a user to a specific post/page, and the information is stored as a Custom Field. The following can be put in functions.php or in a functionality plugin (see: Where to put my code: plugin or functions.php?):

/* Define the custom box */
add_action( 'add_meta_boxes', 'authors_meta_box_wpse_89213' );

/* Do something with the data entered */
add_action( 'save_post', 'save_postdata_wpse_89213', 10, 2 );

function authors_meta_box_wpse_89213() 
{
    global $current_user;
    if( !current_user_can( 'delete_plugins' ) )
        return;
        
    add_meta_box(
        'sectionid_wpse_89213',
        __( 'Page of user' ), 
        'authors_box_wpse_89213',
        'page',
        'side',
        'high'

    );
}

function authors_box_wpse_89213() 
{
    global $post;

    $selected_user = get_post_meta( $post->ID, 'users_dropdown', true);
    $users_list = get_users();

    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'noncename_wpse_89213' );

    echo '<div class="element">
        <select name="users_dropdown" id="users_dropdown">
        <option value="">- Select -</option>';

    foreach( $users_list as $user ) {
          echo '<option value="'.$user->ID.'" ' . selected( $selected_user, $user->ID, false ) . '>'.$user->data->display_name.'</option>';
    }

    echo '</select></div>';
}

function save_postdata_wpse_89213( $post_id, $post_object ) 
{
    // verify if this is an auto save routine. 
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )  
        return;

    // verify this came from the our screen and with proper authorization,
    if ( !isset( $_POST['noncename_wpse_89213'] ) || !wp_verify_nonce( $_POST['noncename_wpse_89213'], plugin_basename( __FILE__ ) ) )
        return;

    // Correct post type
    if ( isset( $_POST['post_type'] ) && 'page' != $_POST['post_type'] ) 
        return;

    // OK, we're authenticated: we need to find and save the data

    //sanitize user input
    $u_id = ( isset( $_POST['users_dropdown'] ) ) ? intval( $_POST['users_dropdown'] ) : false;

    if( $u_id )
        update_post_meta( $post_id, 'users_dropdown', $_POST['users_dropdown'] );
    else
        delete_post_meta( $post_id, 'users_dropdown' );
}

With that in place, you can do something like this in a theme template file to display the "User Page" link:

<?php
if( is_user_logged_in() ) 
{
    global $current_user;
    $args = array(
            'post_type' => 'page',
            'numberposts' => 1,
            'meta_key'=>'users_dropdown',
            'meta_value'=>$current_user->ID,
            'meta_compare'=>'='
        );
    $user_page = get_posts( $args );
    if( $user_page )
    {
        $permalink = get_permalink( $user_page[0]->ID );
        echo "<a href='$permalink'>View your page</a>";
    }
}
?>
brasofilo
  • 22.1k
  • 8
  • 69
  • 264