WooCommerce自定义搜索不起作用

时间:2018-07-30 08:39:00

标签: php woocommerce

我有一个自定义搜索代码,用于搜索WooCommerce订阅的元字段。它有效,但是有一个因某种原因而没有搜索的元?我想知道是否可能是因为其数字?

function custom_search_query( $query ) {
    $custom_fields = array(
        // put all the meta fields you want to search for here
        "gender",
        "barcode",
        "birthdate",
        "employment"
    );
    $searchterm = $query->query_vars['s'];

    // we have to remove the "s" parameter from the query, because it will prevent the posts from being found
    $query->query_vars['s'] = "";

    if ($searchterm != "") {
        $meta_query = array('relation' => 'OR');
        foreach($custom_fields as $cf) {
            array_push($meta_query, array(
                'key' => $cf,
                'value' => $searchterm,
                'compare' => '=='
            ));
        }
        $query->set("meta_query", $meta_query);
    };
}
add_filter( "pre_get_posts", "custom_search_query");

其他字段(性别,出生日期和就业)都可以完美搜索。但是“条形码”并不是出于某种原因,我对此感到困惑。例如,当我删除“生日”时,它不会搜索我是否在搜索框中输入了生日,因此我知道此代码有效。

任何帮助都会很棒!

0 个答案:

没有答案