Skip Navigation

[Resolved] Users to see only their post in cpt

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 3 replies, has 1 voice.

Last updated by MattI4840 1 year, 10 months ago.

Author
Posts
#2533077

Tell us what you are trying to do?

I have a subscription site where users register and are taken directly to a post form to set up a profile which is a cpt. Once they save the profile, they are taken to their profile page, which at this point needs to only be visible to the author. Unfortunately I haven't found any way to ensure this is the case.

I can of course use Access to block logged in users from editing/publishing etc. (although that also does not seem to be working correctly). But there is no option to view or read own only. Currently if a user logs in and types in a URL for another profile while logged in they can see the profile. How can this be achieved?

Also important to note that when I started troubleshooting and attempted to write a custom function to handle this I noticed that for some reason the user who is the author was not being attributed as the author, the admin account was. So I wrote a function to insert the logged in user as the author.

Side note, it would be good to have the author listed in the cpt post grid menu on the back-end. That would have sped up the troubleshooting a lot.

Is there any documentation that you are following?

I've gone over the Access documentation and many support threads to figure this out. As well wordpress docs, stackoverflow threads etc.

#2533169

Update: I was able to get the function working to limit access as described above, and I found the settings to add the author field to the cpt list and update them.

#2533213

I thought I had this fixed, however it appears that even when using the below function to set the author to the current author id, it is returning a 1 when I test. The odd thing is that the author field on the admin side shows the correct name. Here is the code I'm using:

To set the author on submit:

function my_save_data_action2 ($post_id, $form_data) {
if ($form_data['id']==57 || $form_data['id']==50) {
$post= array(
'ID' => $post_id,
'post_author' => get_current_user_id(),
);

wp_update_post( $post );
}
}

add_action('cred_submit_complete', 'my_save_data_action2',10,2);

To redirect if the current user and author don't match:

function redirect_private_draft_portfolios() {

global $post;
$author_id = $post->post_author;

$current_user_id = get_current_user_id();

//variable testing
$text = 'userID: ' . $current_user_id . ' authID: ' . $author_id;
$var_str = var_export($text, true);
$var = "<?php\n\n\$text = $var_str;\n\n?>";
file_put_contents('testing.php', $var);

if($post_type == 'athlete-profile' && !current_user_can('manage_options')) {

if( $current_user_id != $author_id ) {
wp_redirect( home_url() );
exit();
}

}

}
add_action('template_redirect', 'redirect_private_draft_portfolios');

I'm sure I'm missing something, any help you can provide would be very appreciated.

#2533227

It looks like the code is working correctly, the issue was that the post author that was being retrieved/printed was for the homepage (where I was testing from) and not the post that I was requesting. Apologies for the ridiculous conversation with myself here.