You cannot use the function the_title() and the_permalink(), or any other WP functions that start with “the_” in the loop your using. These only work inside a standard WP post loop while (have_posts())...
You need to use alternates like https://developer.wordpress.org/reference/functions/get_the_title/ and https://developer.wordpress.org/reference/functions/get_permalink/ that allow you to specify the post ID.