Hello
This may seem a fairly basic question but I just can't find an answer...
How can I use only let a logged in user access their own CPT posts?.... So for example I have 100 different users who all have a CPT 'About Myself' but I only want the logged in user to access their own post and not the other 99..
It feels like it should be a simple thing!
Regards
Geoff
Dear Geoff,
I assume each 'About Myself' post's author is different WordPress users.
I suggest you try these:
1) Create a WordPress page, for example "Myself", in this page display below post view:
2) Create a post view:
- Query posts of CPT 'About Myself'
- Filter by post author is current logged in user
- Display the post information
So the logged in user can only his own 'About Myself' post in that "Myself" page
See our document:
https://toolset.com/documentation/user-guides/filtering-views-query-by-author/
Hi Luo
Thank you for looking into this for me..
Unfortunately the way my site is set up I have created a template for my CPT 'User Will' and each user is only allowed to create one of these posts... The url is like this:
enlace oculto
enlace oculto
enlace oculto
etc...
So for example user-1 is able to access user-2 and user-3's page...
Best regards
Geoff
In your case, the user "user-1" is post author of post: enlace oculto
So you can use [wpv-conditional] shortcode to check if current logged in user is current post's author, then display what you want.
For example:
[wpv-conditional if="( '[wpv-current-user info="login"]' eq '[wpv-post-author format="meta" meta="user_login"]' ) "]
current user is same as the post author of current post.
[/wpv-conditional]
More help:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-author
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional
Hi Luo
Thanks for getting back to me... Indeed the conditional method would be a good way of controlling what content a user can see on the page but the page would still get generated i.e. enlace oculto would still be accessible to user-1 even though the page would be blank..
The problem with this is that the url rather than carry user-1, user-2 etc actually carry the user's first and surnames i.e.. John-smith etc.. So I need a way that the user isn't allowed to access the page rather than the content..
so a function similar to this:
'If current page is not written by current logged in user then redirect to home page'
Thanks in advance
Geoff
Dear Geoff,
There isn't such kind of built-in feature within Toolset Plugins, you can consider custom codes, for example:
1) When "user-1" open URL:
enlace oculto
Us WordPress action hook "init" to trigger a custom PHP function:
https://developer.wordpress.org/reference/hooks/init/
2) In this custom PHP function, check if current user is the post's author:
https://developer.wordpress.org/reference/functions/get_the_author_meta/
https://developer.wordpress.org/reference/functions/get_current_user_id/
3) If it isn't the post's author, redirect him to the home page:
https://developer.wordpress.org/reference/functions/wp_redirect/
For your reference.
Hi Luo
Thanks for your continued support..
I've written this:
function redirect_user_if_not_author() {
if ($current_user->ID == ! $post->post_author) {
wp_redirect( home_url() );
}}
add_shortcode( 'not_author', 'redirect_user_if_not_author' );
and inserted the shortcode on the template page but it is just redirecting all users including the author of the custom post
What am I doing wrong?...best regards
Geoff
Please try to modify your PHP codes as below, and test again:
add_action( 'template_redirect', function(){
if(
is_singular('user-will') //replace user-will with your custom post type slug
&& get_the_author_meta( 'ID' ) != get_current_user_id()
){
wp_redirect(home_url( '/' )); // redirect to WordPress home URL
}
} );
Hi Luo
Not quite there yet...the code you sent over still re-directs all users to the home page even if the logged in user is the same as the author...so for example
enlace oculto (Myself as the Author)
enlace oculto (A different Author)
both get redirected..
Many thanks
Geoff
I have tried those custom codes in my localhost, it works fine, since it is a custom codes problem, if you need more assistance for it, please provide a test site with the same problem, fill below private message box with website credentials and FTP access, also point out where I can edit your custom PHP codes, I need to test and debug it in a live website. Thanks
Thanks for the details, please try to modify the PHP codes as below:
add_action( 'template_redirect', function(){
if(
is_singular('user-will') //replace user-will with your custom post type slug
&& get_post_field( 'post_author' ) != get_current_user_id()
){
wp_redirect(home_url( '/' )); // redirect to WordPress home URL
}
} );
And test again.
More help:
https://codex.wordpress.org/Function_Reference/get_post_field
My issue is resolved now. Thank you!