2

我想用元字段创建过滤器我用 ACF 插件创建元字段,我想为一些元字段使用或关系,但它们不起作用,例如在过滤器上我为元键训练器选择“男性”和“女性”这告诉我没有帖子,但一个帖子的元值是男性,但我的查询没有显示它们,但是如果我只选择“男性”,这个查询是在帖子中显示帖子元值是 array('Male')

这是用于wordpress上的ajax过滤器

       $args = [
            'posts_per_page' => - 1,
        ];

        if ( $minlength ) {
            $min_lenght_query[] = [
                'key' => 'duration',
                'value' => $minlength,
                'compare' => '>=',
                'type' => 'NUMERIC'
            ];
        }
        if ( $maxlength ) {
            $max_lenght_query[] = [
                'key' => 'duration',
                'value' => $maxlength,
                'compare' => '<=',
                'type' => 'NUMERIC'
            ];
        }
        if ( $minburn ) {
            $minburn_query[] = [
                'key' => 'calorie_burn_minimum',
                'value' => $minburn,
                'compare' => '>=',
                'type' => 'NUMERIC'
            ];
        }
        if ( $maxburn ) {
            $maxburn_query[] = [
                'key' => 'calorie_burn_maximum',
                'value' => $maxburn,
                'compare' => '<=',
                'type' => 'NUMERIC'
            ];
        }
        if ( $difficulty ) {
            $difficulty_query[] = [
                'key' => 'difficulty',
                'value' => $difficulty,
                'compare' => 'IN',
            ];
        }
        if ( $trainer ) {
            foreach ($trainer as $value) {
            $trainer_query[] = [
                'key' => 'trainer',
                'value' => $value,
                'compare' => 'LIKE',
            ];
            }

        }
        if ( $focus ) {
            foreach ($focus as $value) {
            $focus_query[] = [
                'key' => 'body_focus',
                'value' => $value,
                'compare' => 'LIKE',
            ];
            }
        }
        if ( $trainingtype ) {
            foreach ($trainingtype as $value) {
            $trainingtype_query[] = [
                'key' => 'training_type',
                'value' => $value,
                'compare' => 'LIKE',
            ];
            }
        }
        if ( $equipment ) {
            foreach ($trainingtype as $value) {
            $equipment_query[] = [
                'key' => 'equipment',
                'value' => $value,
                'compare' => 'LIKE',
            ];
            }
        }
        if ( $search ) {
            $args['s'] = $search;
        }
        if($sort == 'newest'){
            $args['orderby'] = [ 'date'=>'DESC' ];
        }elseif($sort == 'olders'){
            $args['orderby'] = [ 'date'=>'ASC' ];
        }elseif($sort == 'shortest'){
            $args['orderby'] = [ 'meta_value_num'=>'ASC' ];
            $args['meta_key'] = 'duration';
        }elseif($sort == 'longest'){
            $args['orderby'] = [ 'meta_value_num'=>'DESC' ];
            $args['meta_key'] = 'duration';
        }
$args['meta_query']=array(
        'relation' => 'AND',
        array($min_lenght_query),
        array($max_lenght_query),
        array($minburn_query),
        array($maxburn_query),
        array($difficulty_query),
        array('relation' => 'OR',$trainer_query),
        array('relation' => 'OR',$focus_query),
        array('relation' => 'OR',$trainingtype_query),
        array('relation' => 'OR',$equipment_query),
    );
4

1 回答 1

0

在测试数组后,我发现了如何做到这一点

if ( $trainer ) {
            $trainer_query=array('relation' => 'OR',);
            foreach ($trainer as $value) {
            $trainer_query[] = [
                'key' => 'trainer',
                'value' => $value,
                'compare' => 'LIKE',
            ];
            }

        }

并在元查询中使用它,这解决了我的问题

$args['meta_query']=array(
        'relation' => 'AND',
        array($min_lenght_query),
        array($max_lenght_query),
        array($minburn_query),
        array($maxburn_query),
        array($difficulty_query),
        array('relation' => 'OR',$trainer_query),
        array('relation' => 'OR',$focus_query),
        array('relation' => 'OR',$trainingtype_query),
        array('relation' => 'OR',$equipment_query),
    );
于 2019-06-16T18:37:52.543 回答