'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');
Solution 1:[1]
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);
Solution 2:[2]
You rewrite rule is quite broad and will most likely generate a lot of conflicts
add_action('init', 'mqx_rewrite_tags');
function mqx_rewrite_tags() {
add_rewrite_tag('%tagname%', '([^&]+)');
}
add_action('init', 'mqx_rewrite_rules');
function mqx_rewrite_rules() {
add_rewrite_rule('^shop/(.+)/?$','index.php?page_id=12646&tagname=$matches[1]','top');
}
! important
Remember to flush the rewrite rules; you can do it by going to Settings -> Permalinks and clicking on save button.Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Karthikeyan Ganesan |
| Solution 2 | Qureshi Taha |
