I tested following code to redirect my shop main page to an another page, home.
It works but it also redirects search result to home so people can't see the search result.
add_action('template_redirect', 'bc_010101_redirect_woo_pages');
function bc_010101_redirect_woo_pages() {
if (is_shop())
{
wp_redirect('/');
exit;
}
}
How to resolve it?
CodePudding user response:
You can add is_search() to your if condition, which determines whether the query is for a search.
So you get:
function action_template_redirect() {
/**
* is_shop() - Returns true when on the product archive page (shop).
* is_search() - Determines whether the query is for a search.
*
* (redirect to home)
*/
if ( is_shop() && ! is_search() ) {
wp_safe_redirect( home_url() );
exit;
}
}
add_action( 'template_redirect', 'action_template_redirect' );
OR as a product search adds 's' query variable in URL, you can try to use:
function action_template_redirect() {
/**
* is_shop() - Returns true when on the product archive page (shop).
*
* (redirect to home)
*/
if ( is_shop() && ! isset( $_GET['s'] ) ) {
wp_safe_redirect( home_url() );
exit;
}
}
add_action( 'template_redirect', 'action_template_redirect' );