WordPress Meta Query with Multi Value Arrays

2.1k views Asked by At

I have the following query that I would like to pass an array of values to:

$data = get_posts( array(
    'post_type'   => 'custom_type',
    'post_status' =>  'any',
    'posts_per_page' => 200,
    'meta_query' => array(
    array(
     'key'     => '_customer_names',
     'value'   => $customer_names,
     'compare' => '='
        ),
    array(
     'key'     => '_customer_dates',
     'value'   => $customer_dates,
     'compare' => 'LIKE'
            )
        )
    )
);

For example, I would like to pass like this:

$customer_names = array('John','Tom', 'Simon');
$customer_dates = array('20161225', '20161225', '20161225');

The query will also need to handle the possibility that all customers could have data for each date. In pseudo SQL, my best guess would be:

SELECT * FROM WORDPRESS_POSTS WHERE _customer_names = (John OR Tom OR Simon) AND customer_dates = (20161225 OR 20161226 OR 20161227)

However, at the moment, even when I remove the date restriction, I can't find any posts. Hence, I wanted to confirm my logic is correct.

3

There are 3 answers

3
Pawan Developers On

Try like this:

$data = get_posts( array(
    'post_type'   => 'custom_type',
    'post_status' =>  'any',
    'posts_per_page' => 200,
    'meta_query' => array(
             'relation' => 'AND',
            array(
                'relation' => 'OR',
                array(
                    'key' => '_customer_names',
                    'value' => 'John',
                    'compare' => '='
                ),

                array(
                    'key' => '_customer_names',
                    'value' => 'Tom',
                    'compare' => '='
                ),

                array(
                    'key' => '_customer_names',
                    'value' => 'Simon',
                    'compare' => '='
                )
            ),

            array(
                'relation' => 'OR',
                array(
                    'key' => '_customer_dates',
                    'value' => '20161225',
                    'compare' => '='
                ),

                array(
                    'key' => '_customer_dates',
                    'value' => '20161225',
                    'compare' => '='
                ),

                array(
                    'key' => '_customer_dates',
                    'value' => '20161225',
                    'compare' => '='
                )
            ),
        )
    )
);
0
Noman On

if you want to use array in values you need to use compare with IN clause.

OR if you want to match with exact values you can try @Ravendra Patel's solution.

Also use WP_QUERY because it comes with main query and provide more help to identify the issue

$customer_names = array('John', 'Tom', 'Simon');
$customer_dates = array('20161225', '20161225', '20161225');
$args           = array(
    'post_type'      => 'custom_type',
    'post_status'    => 'any',
    'posts_per_page' => 200,
    'meta_query'     => array(
        array(
            'key'     => '_customer_names',
            'value'   => $customer_names,
            'compare' => 'IN'
        ),
        array(
            'key'     => '_customer_dates',
            'value'   => $customer_dates,
            'compare' => 'IN'
        )
    )
);
$query          = new WP_QUERY($args);
echo 'SQL: '.$query->request.'<br>'; // your query against args
0
Pawan Developers On

try like this:

$customer_names = array('John', 'Tom', 'Simon');
$customer_dates = array('20161225', '20161225', '20161225');

$cm_metaq = array();
foreach($customer_names as $cm){
    $cm_metaq[] = array('key' => '_customer_names', 'value' => $cm, 'compare' => '=');
}
$cd_metaq = array();
foreach($customer_dates as $cd){
    $cd_metaq[] = array('key' => '_customer_dates', 'value' =>$cd,  'compare' => '=');
}


$data = get_posts( array(
    'post_type'   => 'custom_type',
    'post_status' =>  'any',
    'posts_per_page' => 200,
    'meta_query' => array(
             'relation' => 'AND',
            array(
                'relation' => 'OR',
                 $cm_metaq
            ),

            array(
                'relation' => 'OR',
                $cd_metaq
            ),
        )
    )
);