我正在努力改进我的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 );

好的,我的产品已创建,这些属性作为 meta_key=_product_attributesmeta_value= 属性的序列化数组存储在Postmeta中 .
我的搜索表单是:

<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>

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' );

结果我变得静止是搜索帖子 Headers 和发布内容,而不是在产品属性元素中搜索 .

有什么建议?