【问题标题】:Woocommerce product search not checking "product_tag"?Woocommerce 产品搜索未检查“product_tag”?
【发布时间】:2012-08-12 16:42:39
【问题描述】:

WooCommerce 产品的搜索功能似乎没有检查“product_tag”分类术语,也没有检查 SKU 字段?我将 SKU 作为产品标签添加到它们各自的产品中,但是当我搜索 SKU 时它仍然没有返回任何内容......如何使搜索功能检查 product_tag 条款?我已经尝试了很多很多东西,从添加 tax_query 到 pre_get_post 过滤器,到一个全新的 WP_Query 循环,它只是由于某种原因无法搜索 product_tags ......那么产品标签有什么意义???

【问题讨论】:

    标签: wordpress search tags product woocommerce


    【解决方案1】:

    我尝试了 woo 和其他人的代码来搜索 SKU,但没有结果。但是,从存储库下载的插件 search-everything 可以完成这项工作。

    【讨论】:

      【解决方案2】:

      我得到了按 SKU 代码搜索产品的解决方案。

      太简单了,把下面的代码粘贴到function.php中就可以看到搜索结果的变化了。

      function search_by_id_only( $search, &$query_vars ) {
          global $wpdb, $pagenow;
      
          if ( 'edit.php' == $pagenow || empty($search) ) {
              return $search;
          }
      
          $args = array(
              'posts_per_page'  => -1,
              'post_type'       => 'product',
              'meta_query' => array(
                  array(
                      'key' => '_sku',
                      'value' => $query_vars->query['s'],
                      'compare' => 'LIKE'
                  )
              )
          );
          $posts = get_posts( $args );
          if ( empty( $posts ) ) return $search;
          $get_post_ids = array();
          foreach($posts as $post){
              $get_post_ids[] = $post->ID;
          }
          if ( sizeof( $get_post_ids ) > 0 ) {
              $search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $get_post_ids ) . ")) OR (", $search);
          }
          return $search;
      }
      add_filter( 'posts_search', 'search_by_id_only', 999, 2 );
      

      【讨论】:

        猜你喜欢
        • 2021-11-10
        • 2018-11-21
        • 2016-01-06
        • 1970-01-01
        • 1970-01-01
        • 2018-08-28
        • 2016-09-26
        • 2020-12-07
        • 1970-01-01
        相关资源
        最近更新 更多