In you actual code you can see this:
if ( is_user_logged_in() || ! is_page() ) return;
This code does this: if user is logged in or is not in a page, then return/do nothing. You need to remove ! is_page()
:
add_action( 'template_redirect', function() {
if ( is_user_logged_in() ) return;
$restricted = array( 250, 253 ); // all your restricted pages
if ( in_array( get_queried_object_id(), $restricted ) ) {
wp_redirect( site_url( '/user-registration' ) );
exit();
}
});
Alternatively you can leave is_page()
and add also the check for is_category()
as your needs.