Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I’m researching a bit WP_Query and me a problem has arisen.

I have custom fields in tickets and would like to make a WP_Query showing this (put an example):

(field1 = “data1″ AND field2 = “data2″) OR (field1 = “data2″ AND field2 = “data1″)

To do this I created this query but it returns me well.

'meta_query' => array (
    'relation' => 'AND',
    array (
        'key'     => 'field1',
        'value'   => 'data1',
        'compare' => '='
    ),
    array (
        'key'     => 'field2',
        'value'   => 'data2',
        'compare' => '='
    ),

    'relation' => 'OR',
    array (
        'relation' => 'AND',
        array (
            array (
                'key'     => 'field1',
                'value'   => 'data2',
                'compare' => '='
            ),
            array (
                'key'     => 'field2',
                'value'   => 'data1',
                'compare' => '='
            ),
        ),
    ),
)

Does anyone know what I'm wrong? I tried different ways but nothing (I put this because it is what I think is the one that comes closest.

Greetings and thanks!

PD: Sorry for my english! (Google Translate)

share|improve this question

3 Answers 3

up vote 2 down vote accepted

Use the posts_clauses filter for that. Just alter the array parts in there to what you need.

share|improve this answer

Unfortunately, meta queries do not support a combination of AND & OR. But what you can do, is make two queries, one for each rule, then combine the results.

$group_1 = get_posts(
    array(
        'posts_per_page' => -1,
        'fields'         => 'ids',
        'meta_query'     => array (
            array (
                'key'   => 'field1',
                'value' => 'data1',
            ),
            array (
                'key'   => 'field2',
                'value' => 'data2',
            ),
        ),
    )
);

$group_2 = get_posts(
    array(
        'posts_per_page' => -1,
        'fields'         => 'ids',
        'meta_query'     => array (
            array (
                'key'   => 'field1',
                'value' => 'data2',
            ),
            array (
                'key'   => 'field2',
                'value' => 'data1',
            ),
        ),
    )
);

$query = new WP_Query(
    array(
        'post__in' => array_merge( $group_1, $group_2 ),
    )
);
share|improve this answer
    
According the to documentation, meta_query does support relation. –  Eric Holmes Jul 8 '13 at 12:57
1  
@EricHolmes It does indeed, but as I said, not both at once. –  TheDeadMedic Jul 9 '13 at 7:36

You could still do this within a WP_Query delcaration. Use the compare value of IN and pass the value an array of possible values.

$my_query = new WP_Query( array(
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'field1',
            'value' => array('data1', data2'),
            'compare' => 'IN'
        ),
        array(
            'key' => 'field2',
            'value' => array('data1', data2'),
            'compare' => 'IN'
        ),
    ) );

This would also however, return the possibilities of (field1='data1' and field1='data1') or (field1='data2' and field1='data2)', so perhaps this isn't ideal. I'm not sure if that doesn't meet your requirements.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.