Pan*_*Jha 5 php wordpress search wordpress-theming woocommerce
我正在尝试改进我的 WordPress 网站搜索,
我希望用户也能够通过 WooCommerce 产品属性进行搜索。我已经创建了 WooCommerce 产品并通过以下代码添加了所有自定义属性(产品属性):
$attributes = array(
'_image_number' => array(
'name' => 'IMAGE ID',
'value' =>0001,
'is_visible' => '1',
'is_variation' => '0',
'is_taxonomy' => '0'
),
'_year' => array(
'name' => 'YEAR',
'value' => 1988),
'is_visible' => '1',
'is_variation' => '0',
'is_taxonomy' => '0'
),
);
update_post_meta( $new_post_id, '_product_attributes', $attributes );
Run Code Online (Sandbox Code Playgroud)
meta_key=_product_attributes好的,我的产品已创建,这些属性作为我的属性的序列化数组存储在 Postmeta 中meta_value=。
我的搜索表单是:
<form action="<?php echo home_url(); ?>/" method="get" class="wp-search-form">
<i class="oic-zoom"></i>
<input type="text" name="s" id="search" placeholder="<?php echo get_search_query() == '' ? __('Type and hit Enter', 'vp_textdomain') : get_search_query() ?>" />
</form>
<br/>
Run Code Online (Sandbox Code Playgroud)
在functions.php我使用 pre_get_posts 过滤器来修改查询:
function ozy_custom_search( $query ) {
if(!is_admin()) {
if ( isset($query->is_search) && $query->is_search ) {
$query->set( 'post_type', array( 'product', 'post', 'page', 'ozy_portfolio' ) );
$meta_query=array(
'key' => '_product_attributes',
'value' => $query->query['s'],
'compare' => 'EXISTS',
);
$query->set( 'meta_query', $meta_query );
/*
$meta_query=array(
array(
'key' => '_product_attributes',
'value' => $query->query['s'],
'compare' => 'EXISTS',
),
);
*/
// echo "<pre>";print_r($query);die;
}
}
return $query;
};
add_filter( 'pre_get_posts', 'ozy_custom_search' );
Run Code Online (Sandbox Code Playgroud)
我得到的结果仍然是它搜索帖子标题和帖子内容,而不是在产品属性元中搜索。
有什么建议么?
| 归档时间: |
|
| 查看次数: |
2407 次 |
| 最近记录: |