Skip to content

Instantly share code, notes, and snippets.

@codeminelab
Last active December 10, 2018 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeminelab/c6f8a08deae4094aa8e142d15800c31b to your computer and use it in GitHub Desktop.
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
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