Hide custom post type by user roles

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 ) ) {
}

Method 2

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

Method 3

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x