What you’ll want to do is, check if it is a post via is_single()
, get the current URL (or at least that part after the domain), and redirect to the new domain.
Here I am using parse_url()
to easily differentiate between host, path, etc.
add_action('template_redirect', 'wpse_redirect_posts_to_new_page');
function wpse_redirect_posts_to_new_page() {
// if this is not a single post or not of type post, do nothing
if ( ! is_single() || get_post_type() != 'post')
return;
$url = parse_url(get_the_permalink());
$newdomain = 'https://www.example.com'; // no trailing slash!
wp_redirect($newdomain . $url['path']);
exit;
}