WP_Query – Using multiple AND / OR

I have the below PHP code:-

    $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',
                ),
            )
        )   
    );

This will check to see if the search is between the minimum and maximum values which all works perfectly okay.

Read More

Now I want to add a further query which will check if it matches up with a Job Sector, e.g. I added the following below ‘meta_query’:-

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

However, it seems it is just ignoring the above and I’m not sure why. Any help would be much appreciated!

So it looks like this now:-

    <?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 );?>

Related posts

1 comment

  1. The job_type / job_location conditions should be put under ‘meta_query’ I guess:

    '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',
                ),
            ),
        )
    )        
    

Comments are closed.