How to redirect non-logged in users to wordpress login page and bypass some page IDs?

Something like this should do it:

add_action('wp', 'custom_user_redirect');
function custom_user_redirect() {
    if (is_user_logged_in()) {return;}
    // array of page IDs OR slugs to allow access to
    $skippages = array(11, 23, 12); 
    foreach ($pages as $page) {
        if (is_page($page)) {return;}
    }
    wp_safe_redirect(wp_login_url());
}