Hide custom post type by user roles

The 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?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

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.



Method 3

if you want to use the plugin.
Advance 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

Leave a Comment