Support

Account

Home Forums General Issues Multiple relationships in a query

Helping

Multiple relationships in a query

  • Hi.

    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)

  • Hi @guiloviu

    Looking at the code you have posted, there is clearly a problem with the way you have nested your arrays.

    For example, you have

    
    'relation' => 'AND',
    

    and

    
    'relation' => 'OR',
    

    in the same array.

    I think you should take another look at the WP_Query docs and also post this question on stack exchange as it does not involve any ACF specific functions.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Multiple relationships in a query’ is closed to new replies.