MainWP Community

Redirect to a specific page if no product search results are found in WooCommerce

Redirect to a specific page if no product search results are found in WooCommerce on your child site.

Snippet Type

Execute on Child Sites

Snippet

add_action( 'template_redirect', 'no_products_found_redirect' );
function no_products_found_redirect() {
    global $wp_query; 
    if( isset($_GET['s']) && isset($_GET['post_type']) && 'product' === $_GET['post_type'] 
    && ! empty($wp_query) && $wp_query->post_count == 0 ) {
        wp_redirect( get_permalink( 99 ) );
        exit();
    }
}
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.