Change WordPress user roles and capabilities › Forums › How to or FAQ › Hide specific images or images by specific user role or specific user ID › Reply To: Hide specific images or images by specific user role or specific user ID
20/04/2020 at 02:40
#6776
Vladimir
Keymaster
Hi,
You can not make this via URE Pro user interface. Try this custom code. It hides all medial library items uploaded by user with ID=1 from users with roles event-admin or event-superadmin:
add_action('pre_get_posts', 'hide_attachments_for_role', 99);
function hide_attachments_for_role( $query ) {
global $pagenow, $wpdb;
if ( $pagenow !== 'upload.php' ) {
return;
}
if ($query->query['post_type']!=='attachment') {
return;
}
$user = wp_get_current_user();
if ( $user->ID==1 ) {
return; // do not restrict super admin
}
if ( ! (in_array( 'event-admin', $user->roles ) || !in_array( 'event-superadmin', $user->roles ) ) ) {
// do not hide media items uploaded by admin from roles others than above
return;
}
$sub_query = "SELECT ID FROM {$wpdb->posts}
WHERE post_type='attachment' AND post_author=1";
$admin_list = $wpdb->get_col($sub_query);
if ( empty( $query->query['post__in'] ) ) {
if ( empty( $query->query['post__not_in'] ) ) {
$query->set( 'post__not_in', $admin_list );
} else {
$list1 = array_merge( $query['post__not_in'], $admin_list );
$query->set( 'post__not_in', $list1 );
}
} else {
$list1 = array();
foreach ( $query->query['post__in'] as $id ) {
if ( !in_array($id, $admin_list)) {
$list1[] = $id; // leave just allowed items
}
}
if (empty( $list1 ) ) {
$list1[] = -1;
}
$query->set( 'post__in', $list1 );
}
}
// end of hide_attachments_for_role()