Last active
December 10, 2018 10:14
-
-
Save codeminelab/c6f8a08deae4094aa8e142d15800c31b to your computer and use it in GitHub Desktop.
WooCommerce Products Visibility - Redirect 404 pages for not visible products to custom page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function redirect_404_to_custom_page($query) { | |
if ($query->is_single() && $query->is_main_query()) { | |
if (class_exists('WCPV_FRONTEND')) { | |
$wcpv_frontent = WCPV_FRONTEND::get_instance(); | |
if ($wcpv_frontent->rules_are_applied()) { | |
$post_type = $query->query['post_type']; | |
$product_id = $query->query_vars['p']; | |
if ($post_type == 'product' && !in_array($product_id, $wcpv_frontent->include_products)) { | |
wp_redirect('http://www.mysite.com/webpage-to-redirect'); // insert the page you want to redirect to here | |
exit; | |
} | |
} | |
} | |
} | |
} | |
add_action('pre_get_posts', 'redirect_404_to_custom_page'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment