我在合并meta_query“ OR”和“ AND”关系时遇到问题

时间:2019-06-15 22:33:27

标签: php wordpress meta-query

我想用元字段创建过滤器我用ACF插件创建元字段,我想对某些meta_field使用或关联,但是它们不起作用, 例如在过滤器上,我为元密钥教练选择“男”和“女”,这表明我没有帖子,但有一个帖子具有“男”的元值,但我的查询不显示它们,但是如果我仅选择“男”,则此查询为显示帖子 帖子中的元值是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),
    );

1 个答案:

答案 0 :(得分:0)

我在测试阵列后发现了如何做到这一点

var options = {
    method: 'GET',
    url: 'api.themoviedb.org/3/search/multi',
    qs: {
        include_adult: 'false', page: '1',
        query: 'star', language: 'en-US', api_key: 'I CANT SHOW THIS'
    }, body: '{}'
};
request(options, function (error, response, body) {
    if (error) {
        console.log("ERROR!");
        console.log(error);
    } else {
        if (response.statusCode == 200) {
            var parsedBody = JSON.parse(body); 
            console.log(parsedBody["results"][0].name);
        }
    }
});

并在元查询中像这样使用,就可以解决我的问题

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

        }