我扩展了我的搜索范围以包含自定义字段(品牌,颜色),它返回了我期望的所有结果..但是当使用“分层导航”窗口小部件按属性进行过滤时,计数不匹配。我知道返回的产品具有正确的属性,例如 - size:
<div class="sizeavailable">
<?php
$sizevalues = get_the_terms( $product->id, 'pa_size');
foreach ( $sizevalues as $sizevalue ) {
echo $sizevalue->name." ";
}
?>
</div>
即时看到尺寸10,12,14等,但分层导航的数量计数并未反映出这一点。分层导航仅显示产品标题中具有自定义字段术语的产品的属性数据。
因此,在我的测试数据中,我有200个大小为10的产品,但在搜索之后,分层导航列表仅显示5个数字,但点击10个过滤器后,将返回200个产品。
以下是将后元数据加入搜索的函数:
// hook into wp pre_get_posts
add_action('pre_get_posts', 'jc_woo_search_pre_get_posts');
/**
* Add custom join and where statements to product search query
* @param mixed $q query object
* @return void
*/
function jc_woo_search_pre_get_posts($q){
if ( is_search() ) {
add_filter( 'posts_join', 'jc_search_post_join' );
add_filter( 'posts_where', 'jc_search_post_excerpt' );
}
}
/**
* Add Custom Join Code for wp_mostmeta table
* @param string $join
* @return string
*/
function jc_search_post_join($join = ''){
global $wp_the_query;
// escape if not woocommerce searcg query
if ( empty( $wp_the_query->query_vars['wc_query'] ) || empty( $wp_the_query->query_vars['s'] ) )
return $join;
$join .= "INNER JOIN wp_postmeta AS jcmt1 ON (wp_posts.ID = jcmt1.post_id)";
return $join;
}
/**
* Add custom where statement to product search query
* @param string $where
* @return string
*/
function jc_search_post_excerpt($where = ''){
global $product;
global $wp_the_query;
// escape if not woocommerce search query
if ( empty( $wp_the_query->query_vars['wc_query'] ) || empty( $wp_the_query->query_vars['s'] ) )
return $where;
$where = preg_replace("/post_title LIKE ('%[^%]+%')/", "post_title LIKE $1)
OR (jcmt1.meta_key = 'brand' AND CAST(jcmt1.meta_value AS CHAR) LIKE $1)
OR (jcmt1.meta_key = 'colour' AND CAST(jcmt1.meta_value AS CHAR) LIKE $1", $where);
return $where;
}