0

I created a custom post type ( Customers ) and i want to show posts of this post type just to the users with Customer role.

there is any way without or with plugin?

8
  • Are you talking about front-end or back-end?
    – anton
    May 17, 2021 at 15:37
  • in frontend. I have some users in frontend and want to show them some specific posts. and i dont want to another users see this posts May 17, 2021 at 15:40
  • Can you include the template code that displays the custom post-type content and also the registered custom user role's correct name? You should just be able to check the user's role and then determine if you display the custom post type content or if you instead display a message telling them they don't have access, then maybe a login form or a link.... ....it's actually not at all difficult. May 17, 2021 at 15:43
  • @TonyDjukic thanks Tony, yes i think the best way is using "wp_get_current_user" and set an if to check user role. but another thing is if i will do it, bots ( likes google) can see this posts? or i have to hide it also from sitemap or another things? May 17, 2021 at 15:52
  • That's not actually correct, because when you run the condition on the server side (PHP) if the condition isn't met then the content isn't loaded into the page. We're not hiding the content, we're determining which content to load based on whether or not a user is logged in and whether or not they meet the user level criteria. May 17, 2021 at 15:55

3 Answers 3

2

as @Tonydjukic mentioned, the best way is get user role first and then showing content to him.

also with this code we can do it :

$user = wp_get_current_user();
$allowed_roles = array( 'administrator', 'customer' );
if ( array_intersect( $allowed_roles, $user->roles ) ) {
}
3
  • This is the process, now if the condition is met, then you'd drop the_content(); into the if(), but then also add an else {} and include a message like echo 'Sorry mate, you're not allowed to view this until you have <a href="/wp-login.php">signed in</a>.'; May 17, 2021 at 15:57
  • true, we can also redirect users to another pages or something like that. May 17, 2021 at 16:01
  • Exactly. 😊 Once you’ve checked you can perform whatever function you like. May 17, 2021 at 16:04
0

You could use register_post_type_args filter to change public to false depending on your users role.

https://developer.wordpress.org/reference/hooks/register_post_type_args/

https://developer.wordpress.org/reference/functions/register_post_type/#public

0

if you want to use the plugin. Advance Access manager. https://wordpress.org/plugins/advanced-access-manager/

The plugin basically allows you to not only manage access to the backend and front for specific posts. But also hide menu and dashboard entries.

4
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – cjbj
    May 18, 2021 at 6:14
  • agree, i will update the answer
    – MMK
    May 18, 2021 at 7:32
  • Yesterday i tried this plugin but first I have to pay for options that i need them! so code way is better :) May 18, 2021 at 10:11
  • strange, because I know you can do this in the free version. but yeah if you can work with the code it is the best option always.
    – MMK
    May 18, 2021 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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