首页 文章

按产品颜色搜索 - WooCommerce

提问于
浏览
1

我一直在尝试找出用于制作自定义搜索表单的正确$ args-array,允许用户按名称,描述和自定义WooCommerce属性搜索产品(现在通过颜色) .

是否可以使用WP_Query或者我是否需要更改内置搜索功能?如果是这样 - 怎么样?

这是我现在一直在尝试的$ args-options:

$args = array(
    'posts_per_page' => 20,
    'no_found_rows' => true,
    'post_type' =>  array('product'),
    'tax_query' => array(
        array(
            'taxonomy' => 'oct-search',
            'field' => 'slug',
            'terms' => array($_POST["search_string"]),
        ),
    ),
);

1 回答

  • 1

    好的,所以我自己解决了这个问题,使用这段代码:

    $attributes =  'oct-shade';
    $attributes = 'pa_'.$attributes;
    $filters = explode(',', $_POST["search_string"]);
    $args = array(
        'posts_per_page' => 20,
        'no_found_rows' => true,
        'post_type' =>  array('product'),
        'tax_query' =>
        array(
            'relation' => 'OR',
            array(
                'taxonomy'      => "$attributes",
                'field'         => 'slug',
                'terms'         => $filters,
                'operator'      => 'IN'
            ),
        ),
    );
    

相关问题