最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Redirect to a page while maintaining search query parameters without causing an infinite loop

programmeradmin8浏览0评论

We are making use of a plugin called Product Filter by XforWoocommerce. It has a feature that limits the available product filter options based on what products are visible, however, it does not work on search queries. Rather than try to make a large custom edit that is potentially difficult to maintain I came up with this solution:

Any time a search query is made, redirect to the main product category page that matches most of the results.

We are making use of a plugin called Product Filter by XforWoocommerce. It has a feature that limits the available product filter options based on what products are visible, however, it does not work on search queries. Rather than try to make a large custom edit that is potentially difficult to maintain I came up with this solution:

Any time a search query is made, redirect to the main product category page that matches most of the results.

Share Improve this question asked Sep 25, 2020 at 14:08 fudgefudge 1173 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I achieved this with a couple filters:

add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{   
    $vars[] = 'r'; // var1 is the name of variable you want to add       
    return $vars;
}

function redir_cat_match($a) {
   global $wp_query;
   if ($wp_query->is_main_query() && $wp_query->is_search()){
      if ($wp_query->query_vars['s'] != ""){
         if ($_GET['r'] != '0'){
            if (!empty($wp_query->posts[0])){
               $s_value = $wp_query->query_vars['s'];
               $terms = get_the_terms( $wp_query->posts[0]->ID, 'product_cat' );
               $link = get_term_link($terms[0]->term_id) . "?s=" . htmlspecialchars($s_value) . "&r=0";
               wp_redirect($link );
               exit;
            }
         }
      }
   }
}
add_filter('template_redirect','redir_cat_match');

In order for this to work, you need some way for the snippet to detect that a redirect has already occurred. I did this by adding variable r as a potential parameter. In order to retrieve this, you have to use $_GET['r'].

I'm not sure if this is the most efficient way to do this, but it performs ok during testing and gets the desired results. Realistically, it's effectiveness would rely on the quality of the search results so if you're using a plugin like SearchWP or even an ElasticSearch implementation you'll get the best results.

发布评论

评论列表(0)

  1. 暂无评论