Hide custom post type by user roles
-
20-05-2021 - |
Question
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?
La solution
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 ) ) {
}
Autres conseils
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
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.