I am going to recommend you reorganize your thinking. Instead of having posts generate a user login (which is complicated, but can be done), I think you can associate a post to a user.
Unfortunately, it doesn't seem there are any plugins that will do this for you. I wrote this quick plugin you can use to get you started. This has not been tested, so use at your own risk and be careful.
The plugin outlined below does two things:
- Adds a field to all user profiles for the administrator allowing you to tie a user to a private post. Note: This only shows private posts, so the post in question must be private.
- Detects a page with a slug "client" and redirects the user to their private post if the administrator has set their private post in their profile.
Steps to install:
- Upload the above into the plugins folder:
/wp-content/plugins/your-custom-plugin/custom-plugin.php
.
- Login to wp-admin, activate "Custom Client-Post Login".
- Create a page called "Client".
- Create a private post (e.g., "Client A's Private Post").
- Create or edit a user login for your client. Scroll to the bottom of their profile and you'll see "Client Post Association". Choose the post you want them to see when visiting example.com/client/
- Login as your client and test by visiting example.com/client/
Be sure to comment with any questions you may have.
<?php
/*
Plugin Name: Custom Client-Post Login
Description: Each user as a private post linked to it. When visiting /clients, they are redirected.
Version: 1.0
*/
/*
Setup Steps:
1. Create page called "Client".
2. Create two or three posts that are marked as "PRIVATE".
3. Create two or three users.
4. When editing users, you can set their custom post in their user profile.
5. When that user is logged in, and visits the client page, they will be redirected to their private post.
*/
/*
CODE FROM https://developer.wordpress.org/plugins/users/working-with-user-metadata/#example-form-field
*/
/**
* The field on the editing screens.
*
* @param $user WP_User user object
*/
function sfusrcust_usermeta_form_field_client_post($user){
$options = "";
$chosen_post_id = get_user_meta($user->ID, '_client_post', true);
$posts = get_posts(array(
'post_type' => 'post',
'post_status' => 'private',
'showposts' => '-1'
));
if(count($posts) > 0){
foreach($posts as $post){
$options .= sprintf('<option value="%s" %s>%s</option>', $post->ID, ($chosen_post_id ? 'selected': ''), $post->post_title);
}
}
if(!current_user_can('administrator')){
echo sprintf('<input type="hidden" name="client_post" id="client_post" value="%s" />', $chosen_post_id);
return;
}
?>
<h3>Client Post Association</h3>
<table class="form-table">
<tr>
<th>
<label for="client_post">Select the users post.</label>
</th>
<td>
<?php if($options !== ''){ ?>
<select name="client_post" id="client_post">
<?php echo $options; ?>
</select>
<?php } else { echo 'No private posts were found.'; } ?>
<p class="description">
This allows you, the administrator, to select the post that the client will see when visiting the client page.
</p>
</td>
</tr>
</table>
<?php
}
add_action('edit_user_profile', 'sfusrcust_usermeta_form_field_client_post');
add_action('show_user_profile', 'sfusrcust_usermeta_form_field_client_post');
add_action('personal_options_update', 'sfusrcust_usermeta_form_field_client_post_update');
add_action('edit_user_profile_update', 'sfusrcust_usermeta_form_field_client_post_update');
function sfusrcust_usermeta_form_field_client_post_update($user_id)
{
// check that the current user have the capability to edit the $user_id
if (!current_user_can('edit_user', $user_id)) {
return false;
}
// create/update user meta for the $user_id
return update_user_meta(
$user_id,
'_client_post',
$_POST['client_post']
);
}
/*
Code From: https://developer.wordpress.org/reference/hooks/pre_get_posts/
*/
add_action( 'pre_get_posts', 'pre_get_posts_client_page' );
function pre_get_posts_client_page( $query ) {
/*
NOTE: The only reason this works is because of the "client" slug. If you need to change that, you will have to change the slug below as well.
*/
if ( ! is_admin() && is_user_logged_in() && is_page('client') ) {
$user_post_id = get_user_meta(get_current_user_id(), '_client_post', true);
if($user_post_id){
$query->set('p', $user_post_id);
}
}
}