【问题标题】:Make woocommerce product search only search certain fields使 woocommerce 产品搜索仅搜索某些字段
【发布时间】:2014-06-12 11:39:24
【问题描述】:

我刚刚使用 Woocommerce (2.1.11) 和他们的免费 Mystile 主题建立了一个专业书店网站。产品(书籍)添加了几个自定义字段,包括 isbn、作者、出版商等。

我现在想修改默认产品搜索工具以仅搜索自定义字段(而不是仅在搜索中包含自定义字段)。有谁知道这是怎么做到的吗?

谢谢

【问题讨论】:

  • 您是否尝试在 wordpress 插件中搜索 [likn]wordpress.org/plugins/woocommerce-custom-field-product-search/… 这将提供自定义字段搜索
  • 我试过几个插件,包括那个。他们所做的只是将自定义字段添加到搜索中 - 以及标题、描述等。我需要的是只搜索自定义字段的东西。你知道有这样的吗?

标签: php wordpress woocommerce


【解决方案1】:

再次感谢 Dinesh - 最终到达那里 - 结果如下

WP 模板是一回事 - 对此感到满意 - 只是对 Woocommerce 有点困惑。

最终结果: 由于 Woocommerce 搜索结果被输入到他们的插件特定的“archive-product.php”页面中,我不得不在那里添加你的代码。 archive-pruduct.php 也是主要类别列表页面,因此在使用上述覆盖之前需要使用 is_search 条件捕获搜索:

我是这样做的:

<?php 
            if ( is_search() ) :
                $args = array(
                    'post_type' => 'product',
                    'posts_per_page' => -1,
                    'order' => 'ASC',
                    'meta_query' => array(
                         'relation' => 'OR',
                             array(
                              'key' => 'meta:_isbn_field',               //your meta key
                              'value' => $_REQUEST['meta:_isbn_field'],  //your search post value
                              'compare' => 'LIKE'
                              )
                              ), array(
                                  'key' => 'meta:_published_field',               //your meta key
                                  'value' => $_REQUEST['meta:_published_field'],  //your search post value
                                  'compare' => 'LIKE'
                              ),
                              array(
                                  'key' => 'meta:_publisher_field',              //your meta key
                                  'value' => $_REQUEST['meta:_publisher_field'],  // your search post value for author
                                  'compare' => 'LIKE'
                              ),
                              array(
                                  'key' => 'post_title',
                                  'value' => $_REQUEST['post_title'], 
                                  'compare' => 'LIKE'
                              )
                        );


                $the_query = new WP_Query( $args );

                if ( $the_query->have_posts() ) {
                     echo '<h2>Your search Results</h2>';

                        while ( $the_query->have_posts() ) {
                            $the_query->the_post();
                            echo wc_get_template_part( 'content', 'single-productsimple' );
                    }
                }else{

                    echo 'sorry nothing found';
                }
            else:

                //leave default archive-product.php code here

            endif;
            ?>

【讨论】:

    【解决方案2】:

    您可以尝试使用 meta_query 进行此搜索,因为您知道自定义字段会保存在 wp_postmeta 表中。

    所以只要做这样的事情:

    <?php
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
        'order' => 'ASC',
        'meta_query' => array(
                         'relation' => 'OR',
                         array(
                          'key' => ' isbn',               //your meta key
                          'value' => $_REQUEST['isbn'],  //your search post value
                          'compare' => 'LIKE'
                          ),
                          array(
                          'key' => ' author',              //your meta key
                          'value' => $_REQUEST['author'],  // your search post value for author
                          'compare' => 'LIKE'
                          ),
                          array(
                          'key' => ' publisher ',
                          'value' => $_REQUEST['publisher '], 
                          'compare' => 'LIKE'
                          )
                          )
                );
    
    
    $query = new WP_Query( $args );
    
    if (have_posts()) : while (have_posts()) : the_post();
    
    // your result go here
    ?> 
    

    【讨论】:

    • 谢谢 Dinesh,但我不知道在哪里放置您的代码。我在 Woocommerce 搜索结果页面中假设它是一个循环,但我看不出它是如何工作的。能不能再详细一点?
    • 刚刚了解,如何在wordpress中制作自定义页面模板......codex.wordpress.org/Page_Templates
    【解决方案3】:

    使用名称和产品描述进行搜索的自定义代码(只需将此代码粘贴到 function.php 中)

    // Join in your where condition
    
    function cf_search_join( $join ) {
    
        global $wpdb;
    
        if ( is_search() ) {    
            $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
        }
    
        return $join;
    }
    
    add_filter('posts_join', 'cf_search_join' );
    
    
    function cf_search_distinct( $where ) {
    
        global $wpdb;
    
        if ( is_search() ) {
            return "DISTINCT";
        }
    
        return $where;
    }
    
    add_filter( 'posts_distinct', 'cf_search_distinct' );
    
    
    
    // Search by Post Title
    
    function search_by_id_only( $search, &$wp_query )
    {
    
        //remove_filter( 'posts_search', 'vc_search_by_title_only', 500, 2 );
    
        global $wpdb,$wp_the_query,$wp;
    
        if ( empty( $search ) ){
            return $search; // skip processing - no search term in query
        }
            $q = $wp_query->query_vars;
    
            $n = ! empty( $q['exact'] ) ? '' : '%';
            $search = $searchand = '';
    
            foreach ( (array) $q['search_terms'] as $term ) {
    
                $term = $wpdb->esc_like( $term );
          $n = '%';
    
                $like = $n . $term . $n;
                $search .= $wpdb->prepare( "{$searchand}($wpdb->posts.ID LIKE %s)", $like );
    
                $searchand = ' OR ';
    
                                $search .= $wpdb->prepare( "{$searchand}($wpdb->posts.post_title LIKE %s)", $like );
                $searchand = ' OR ';
    
        $search .= $wpdb->prepare( "{$searchand}($wpdb->postmeta.meta_key = '_sku' AND CAST($wpdb->postmeta.meta_value AS CHAR) LIKE %s)", $like );
    
                //$search .= $wpdb->prepare("{$searchand} (select * from {$wpdb->postmeta} where post_id={$wpdb->posts}.ID and meta_key in ('_sku') and meta_value like %s )",$like);
                $searchand = ' OR ';
    
        //$search .= $wpdb->prepare( "{$searchand}($wpdb->posts.post_content LIKE %s)", $like );
    
        //      $searchand = ' OR ';
            }
            if ( ! empty( $search ) ) {
                $search = " AND ({$search}) ";
    
            }   
        return $search;
        
    }
    
    if( !is_admin() ){
    
      add_filter( 'posts_search', 'search_by_id_only', 500, 2 );
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-31
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      相关资源
      最近更新 更多