function b2b_redirect_if_product_is_not_for_user() { if ( is_product() ) { $allowed_user = get_post_meta( get_the_ID(), '_allowed_user', true ); if ( $allowed_user != get_current_user_id() ) { wp_redirect( site_url( '/shop' ), 301 ); } } } add_action('template_redirect', 'b2b_redirect_if_product_is_not_for_user');