Support

Account

Home Forums ACF PRO Gallery field hide media library Reply To: Gallery field hide media library

  • I created a custom user role with capabilities similar to ‘author’. The code you posted limited the images visible in the Gallery field itself but not in the media library. I was able to get it working with the following:

    function restrict_media_library_to_current_user( $wp_query_obj ) {
    
    	if ( ! current_user_can( 'level_5' ) ) {
    
    		global $current_user, $pagenow;
    
    		if (  ! is_a( $current_user, 'WP_User' ) || 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' ) {
    			return;
    		}
    
    		$wp_query_obj->set( 'author', $current_user->ID );
    
    		return;
    	}
    }
    
    add_action( 'pre_get_posts', 'restrict_media_library_to_current_user' );