In WordPress, I created a new User role called "Customers" to only see the pages in the wp-admin Dashboard that only that role has created and not all the pages that are created by other Customers or the Administrator. I mean every Customer would see their own created pages only. I have installed Adminimize, Ultimate Member, PublishPress Permissions, and User Role Editor plugins. But I couldn't do it. Any assistance would be highly appreciated.
-
Please do not post the same question in multiple sites. Originally posted here: stackoverflow.com/questions/76666381/…– CalebCommented Jul 12, 2023 at 0:02
-
Apology. I have deleted the question from stackoverflow and it will reside here.– Programming GeekCommented Jul 12, 2023 at 0:04
Add a comment
|
1 Answer
Add the following to an mu-plugin (custom user role is so76666381
) (tested):
// Adjust admin queries to only show pages by current user if has custom role.
add_action( 'pre_get_posts', static function ( $query ) {
// Limit hiding pages only to admin queries.
if ( ! is_admin() ) {
return;
}
// Get post types from query.
$post_types = ( array ) $query->get( 'post_type', array() );
// If pages are not being queried, bail.
if ( ! in_array( 'page', $post_types ) ) {
return;
}
// Get user's current role(s).
$roles = wp_get_current_user()->roles;
// If user does not have custom role, bail.
if ( ! is_array( $roles ) || ! in_array( 'so76666381', $roles ) ) {
return;
}
// Limit results to those by current user with custom role.
$query->set( 'author', get_current_user_id() );
} );
-
Thank you Sir. This worked. But now the problem is that when a new "Customer" registers him/ herself, they see all the pages until and unless they create one of their own. Once they add a new page, then they see only that page and the others don't appear then. Commented Jul 12, 2023 at 5:16
-
Sir, I modified this line as:
if ( ! is_array( $roles ) || ( ! in_array( 'customers', $roles ) && ! is_user_logged_in() ) ) { return; }
and its working perfectly. Thanks a ton! Commented Jul 12, 2023 at 5:27