WP_Query - 使用多个AND / OR

时间:2016-01-27 16:11:23

标签: php wordpress wp-query

我有以下PHP代码: -

    $args = array(
        'posts_per_page'=> -1,
        'post_type'     => 'jobs',
        'order'             => 'ASC',
        's' => $search_field,
        'meta_query'    => array(
            'relation' => 'OR',
            array(
                'relation' => 'AND',
                array(
                    'key'       => 'job_salary_to',
                    'value'     => array($job_salary_from,$job_salary_to),
                    'type'    => 'numeric',
                    'compare'   => 'BETWEEN',
                ),
            ),
            array(
                'relation' => 'AND',
                array(
                    'key'       => 'job_salary_from',
                    'value'     => array($job_salary_from,$job_salary_to),
                    'type'    => 'numeric',
                    'compare'   => 'BETWEEN',
                ),
            )
        )   
    );

这将检查搜索是否在最小值和最大值之间,这些值完全正常。

现在我想添加另一个查询,该查询将检查它是否与作业扇区匹配,例如我在下面添加了以下内容' meta_query': -

            'relation' => 'AND',
            array(
                'key'       => 'job_sector',
                'value'     => 'Finance',
                'compare'   => 'LIKE',
            ),

然而,似乎只是忽略了上述内容,我不确定为什么。任何帮助将不胜感激!

现在看起来像这样: -

    <?php
    $args = array(
        'posts_per_page'=> -1,
        'post_type'     => 'jobs',
        'order'             => 'ASC',
        's' => $search_field,
        'meta_query'    => array(
            'relation' => 'OR',
            array(
                'relation' => 'AND',
                array(
                    'key'       => 'job_salary_to',
                    'value'     => array($job_salary_from,$job_salary_to),
                    'type'    => 'numeric',
                    'compare'   => 'BETWEEN',
                ),
            ),
            array(
                'relation' => 'AND',
                array(
                    'key'       => 'job_salary_from',
                    'value'     => array($job_salary_from,$job_salary_to),
                    'type'    => 'numeric',
                    'compare'   => 'BETWEEN',
                ),
            ),
        ),  
        'relation' => 'AND',
        array(
            'key'       => 'job_type',
            'value'     => $job_type,
            'compare'   => 'LIKE',
        ),
        array(
            'key'       => 'job_location',
            'value'     => $job_location,
            'compare'   => 'LIKE',
        )
    );

    $fetch_jobs = new WP_Query( $args );?>

1 个答案:

答案 0 :(得分:2)

job_type / job_location条件应放在&#39; meta_query&#39;我想:

'meta_query' => array(
    'relation' => 'AND',
    array(
        array(
            'key'       => 'job_type',
            'value'     => $job_type,
            'compare'   => 'LIKE',
        ),
        array(
            'key'       => 'job_location',
            'value'     => $job_location,
            'compare'   => 'LIKE',
        ),
    ),
    array(
        'relation' => 'OR',
        array(
            'relation' => 'AND',
            array(
                'key'       => 'job_salary_to',
                'value'     => array($job_salary_from,$job_salary_to),
                'type'    => 'numeric',
                'compare'   => 'BETWEEN',
            ),
        ),
        array(
            'relation' => 'AND',
            array(
                'key'       => 'job_salary_from',
                'value'     => array($job_salary_from,$job_salary_to),
                'type'    => 'numeric',
                'compare'   => 'BETWEEN',
            ),
        ),
    )
)