Skip to content
Advertisement

How do I stop WordPress from redirecting certain links automatically on homepage?

I want to display filtered results on the homepage of a WooCommerce shop by editing the URL.

I have a filtering option by tag, category and price range.

The URL for filtering by tag would look like this:

sitename.com/shop/?product_tag=tag1

And that redirects automatically to

sitename.com shop/product-tag/tag1/

Which I don’t want because I want to add other filtering options, for example, like this:

sitename.com/shop/?product_tag=tag1&product_cat=category1

If I put this filter on a different page than the /shop/, it doesn’t redirect and display the results there, on the page.

How can I disable this annoying WP redirect?

I tried this following one but didn’t work:

remove_filter('template_redirect','redirect_canonical');

Advertisement

Answer

function custom_rewrite_basic() 
{
  add_rewrite_rule('^shop/?$', 'index.php?page_id={your_page_id for this page}', 'top');
}
add_action('init', 'custom_rewrite_basic');

or use the following code

function redirect_prevent()
{
add_action('redirect_canonical','__return_false');
}
add_action('template_redirect','redirect_prevent',1);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement