Perhaps something like this?
add_action( 'template_redirect', 'not_logged_in_redirect_home' );
add_action( 'do_feed', 'not_logged_in_redirect_home' );
function not_logged_in_redirect_home(){
if ( is_user_logged_in() ){
return false;
}
if (
! is_home() // use this option if you show blogs posts on the home page
// ! is_front_page() // use this if you show a static page
){
wp_redirect( home_url() );
exit;
}
}
Check the documentation on is_home() – https://developer.wordpress.org/reference/functions/is_home/
Since WordPress 2.1, when the static front page functionality was introduced, the blog posts index and site front page have been treated as two different query contexts, with is_home() applying to the blog posts index, and is_front_page() applying to the site front page
So, you might need to use is_front_page() – depending on your setup.