Wordpress WooCommerce成员资格-按自定义元密钥搜索无效

Wordpress WooCommerce成员资格-按自定义元密钥搜索无效,wordpress,woocommerce,Wordpress,Woocommerce,我正在尝试实现对WooCommerce会员自定义元密钥的搜索。此处的电话号码通过获取post类型wc_用户_会员的自定义元密钥“_billing_Phone”来显示。知道它为什么不起作用吗 指 功能高级\管理\自定义\搜索($pieces,WP\u Query$WP\u Query){ 全球$wpdb; 如果(!is_admin())返回$pieces; 如果(!array_key_存在('post_type',$wp_query->query))返回$parties; //如果不是正确的立柱

我正在尝试实现对WooCommerce会员自定义元密钥的搜索。此处的电话号码通过获取post类型wc_用户_会员的自定义元密钥“_billing_Phone”来显示。知道它为什么不起作用吗

功能高级\管理\自定义\搜索($pieces,WP\u Query$WP\u Query){
全球$wpdb;
如果(!is_admin())返回$pieces;
如果(!array_key_存在('post_type',$wp_query->query))返回$parties;
//如果不是正确的立柱类型,则应将其取出
如果('wc\U用户\U成员资格'==$wp\U查询->查询['post\U类型'])){
//$wp_query->set('post_type','wc_user_membership');
//搜寻
if(isset($wp\u query->query['s'])){
//在此添加您的acf字段
$list_searchable_acf=数组(“我的acf_字段名称”);
//获取搜索表达式
$terms=$wp_query->query_vars['s'];
//分解搜索表达式以获取搜索词
$exploded=exploded(“”,$terms);
如果($exploded==FALSE | |计数($exploded)==0)
$exploded=数组(0=>$terms);
//重置搜索以便在我们呼啸时重建它
$where=$pieces['where'];
foreach($分解为$标记):
$where.=”
还是存在(
从“$wpdb->postmeta”中选择*
其中post_id=“.$wpdb->posts.”id
和“$wpdb->posts.”。post\u type='wc\u user\u membership'
及(“;
foreach($list_searchable_acf作为$searchable_acf):
$where.=“(meta_键类似“%”,$searchable_acf.%”和meta_值REGEXP'[[::]]”);
endforeach;
$where.=”)
)";
endforeach;
//替换where子句
$pieces['where']=$where;
}
}
退回$5件;
}
添加过滤器('posts\u子句','advanced\u admin\u custom\u search',500,2);
指的是

功能高级\管理\自定义\搜索($pieces,WP\u Query$WP\u Query){
全球$wpdb;
如果(!is_admin())返回$pieces;
如果(!array_key_存在('post_type',$wp_query->query))返回$parties;
//如果不是正确的立柱类型,则应将其取出
如果('wc\U用户\U成员资格'==$wp\U查询->查询['post\U类型'])){
//$wp_query->set('post_type','wc_user_membership');
//搜寻
if(isset($wp\u query->query['s'])){
//在此添加您的acf字段
$list_searchable_acf=数组(“我的acf_字段名称”);
//获取搜索表达式
$terms=$wp_query->query_vars['s'];
//分解搜索表达式以获取搜索词
$exploded=exploded(“”,$terms);
如果($exploded==FALSE | |计数($exploded)==0)
$exploded=数组(0=>$terms);
//重置搜索以便在我们呼啸时重建它
$where=$pieces['where'];
foreach($分解为$标记):
$where.=”
还是存在(
从“$wpdb->postmeta”中选择*
其中post_id=“.$wpdb->posts.”id
和“$wpdb->posts.”。post\u type='wc\u user\u membership'
及(“;
foreach($list_searchable_acf作为$searchable_acf):
$where.=“(meta_键类似“%”,$searchable_acf.%”和meta_值REGEXP'[[::]]”);
endforeach;
$where.=”)
)";
endforeach;
//替换where子句
$pieces['where']=$where;
}
}
退回$5件;
}
添加过滤器('posts\u子句','advanced\u admin\u custom\u search',500,2);
add_filter( 'woocommerce_wc_user_membership_search_fields', 'billing_phone_searchable_field' );
function billing_phone_searchable_field( $meta_keys ){
    $meta_keys[] = '_billing_phone';
    return $meta_keys;
}
function advanced_admin_custom_search($pieces, WP_Query $wp_query) {
global $wpdb;

if (!is_admin() ) return $pieces;

if (!array_key_exists('post_type', $wp_query->query)) return $pieces;

// bail out if not the correct post type
if ( 'wc_user_membership' === $wp_query->query['post_type'] ) {
   
    // $wp_query->set( 'post_type',  'wc_user_membership' );

    // search
    if ( isset( $wp_query->query['s'] ) ) {
        
        // add here your acf fields
        $list_searcheable_acf = array("my_acf_field_name");

        // get search expression
        $terms = $wp_query->query_vars[ 's' ];
                
        // explode search expression to get search terms
        $exploded = explode( ' ', $terms );
        if( $exploded === FALSE || count( $exploded ) == 0 )
            $exploded = array( 0 => $terms );
            
        // reset search in order to rebuilt it as we whish
        $where = $pieces['where'];
        
        foreach( $exploded as $tag ) :
            $where .= " 
           OR EXISTS (
                SELECT * FROM " . $wpdb->postmeta . "
                    WHERE post_id = " . $wpdb->posts . ".ID
                        AND " . $wpdb->posts . ".post_type = 'wc_user_membership'
                        AND (";
            foreach ($list_searcheable_acf as $searcheable_acf) :
                $where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value REGEXP '[[:<:]]{$tag}[[:>:]]') ";
            endforeach;
                $where .= ")
            )";
        endforeach;

        // replace the where clauses
        $pieces['where'] = $where;
    }
}

return $pieces;
}


add_filter( 'posts_clauses', 'advanced_admin_custom_search', 500, 2 );