This code will do what you want. It will redirect to the latest product added if you add ?latest_product
to your site’s URL. So the dynamic link would be http://example.com/?latest_product
function wpse_274155_redirect_latest_product() {
// If URL has ?latest_product query string.
if ( isset( $_GET['latest_product'] ) ) {
// Get latest product.
$latest_products = get_posts( array(
'posts_per_page' => 1,
'post_type' => 'product',
'no_found_rows' => true,
'orderby' => 'date',
'order' => 'DESC',
) );
// Make sure product exists.
if ( isset( $latest_products[0] ) ) {
// Redirect to product.
wp_redirect( get_permalink( $latest_products[0] ) );
exit();
}
}
}
add_action( 'template_redirect', 'wpse_274155_redirect_latest_product' );