我刚刚用Woocommerce(2.1.11)和他们免费的Mystile主题建立了一个专业的书店网站。产品(图书)添加了几个自定义字段,包括isbn、作者、出版商等。
我现在想修改默认的产品搜索工具,只搜索自定义字段(而不是只在搜索中包含自定义字段)。有人知道怎么做吗?
谢谢
再次感谢Dinesh -最终做到了-结果如下
WP模板是一件事-很高兴-只是有点困惑Woocommerce。
最终结果:由于Woocommerce的搜索结果被输入到插件特定的"archive-product.php"页面,我不得不在那里添加你的代码。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;
?>
您可以尝试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
?>
使用名称和产品描述进行搜索的自定义代码(只需将此代码粘贴到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 );
}