WordPress.org

Ready to get started?Download WordPress

Forums

Multiple relationships in a query (2 posts)

  1. guiloviu
    Member
    Posted 1 year ago #

    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)

  2. vtxyzzy
    Member
    Posted 1 year ago #

    I think you can only have one 'relation' parameter in a meta_query. I do not think you can do what you want with the standard query parameters. You will either need to use filters to modify the query, or code an SQL statement and use that for the query.

Topic Closed

This topic has been closed to new replies.

About this Topic