2014-06-12 67 views
1

我剛剛建立了一個專門的書店網站使用Woocommerce(2.1.11)與他們的免費Mystile主題。該產品(書)有幾個自定義字段添加包括isbn,作者,出版商等。使woocommerce產品搜索只搜索某些領域

我想現在修改默認產品搜索設施,只搜索自定義字段(而不是隻包括在搜索中的自定義字段)。有誰知道這是怎麼做到的嗎?

感謝

+0

沒有嘗試在WordPress插件搜索 [likn] https://wordpress.org/plugins/woocommerce-custom-field-product-search/screenshots/ [/鏈接]這將提供自定義字段搜索 – Vijayaragavendran

+0

我試過了幾個插件,包括那個。他們所做的只是向搜索添加自定義字段 - 以及標題,說明等。我需要的是僅搜索自定義字段的內容。你知道這樣做嗎? – Screenbeetle

回答

0

你可以嘗試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 
?> 
+0

謝謝Dinesh,但我不確定在哪裏放置代碼。我假設在Woocommerce搜索結果頁面給出了它的一個循環,但我不明白這是如何工作的。你能給我多一點細節嗎? – Screenbeetle

+0

剛剛瞭解,如何在WordPress中製作自定義頁面模板......... http://codex.wordpress.org/Page_Templates – Dinesh

1

再次感謝迪內希 - 到了那裏到底 - 以下

WP模板的結果是一兩件事 - 快樂與 - 只要Woocommerce略有困惑。

最終結果是: 作爲Woocommerce搜索結果被送入他們的插件特定的「存檔product.php」我不得不在此加入您的代碼頁。歸檔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; 
      ?> 
0

//自定義代碼搜索使用名稱和產品說明(只需粘貼function.php這個代碼)

在WHERE條件//加入

功能cf_search_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');

功能cf_search_distinct($其中){

global $wpdb; 

if (is_search()) { 
    return "DISTINCT"; 
} 

return $where; 

}

的add_filter( 'posts_distinct', 'cf_search_distinct');

//按文章標題搜索

功能search_by_id_only($搜索,& $ 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; 

}

如果(!is_admin()){

add_filter('posts_search','search_by_id_only',500,2);

}