我刚刚使用Woocommerce(2.1.11)建立了一个专业的书店网站,其免费的Mystile主题。产品(书籍)添加了几个自定义字段,包括isbn,作者,出版商等。
我现在想修改默认的产品搜索工具,只搜索自定义字段(而不是在搜索中包含自定义字段)。有谁知道这是怎么做到的吗?
由于
答案 0 :(得分: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;
?>
答案 1 :(得分: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
?>
答案 2 :(得分:0)
//使用名称和产品描述进行搜索的自定义代码(只需将此代码粘贴到function.php中)
//加入你的条件
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(&#39; posts_join&#39;,&#39; cf_search_join&#39;);
功能cf_search_distinct($ where){
global $wpdb;
if ( is_search() ) {
return "DISTINCT";
}
return $where;
}
add_filter(&#39; posts_distinct&#39;,&#39; cf_search_distinct&#39;);
//按帖子标题搜索
function search_by_id_only($ search,&amp; $ 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(&#39; posts_search&#39;,&#39; search_by_id_only&#39;,500,2);
}