0

Is it possible to create complex query with WP User Query ?

With WP Meta Query, we can make complex queries with Nested arrays like :

$meta_query_args = array(
    'relation' => 'OR', // Optional, defaults to "AND"
    array(
        'key'     => '_my_custom_key',
        'value'   => 'Value I am looking for',
        'compare' => '='
    ),
    array(
        'relation' => 'AND',
        array(
            'key'     => '_my_custom_key_2',
            'value'   => 'Value I am looking for 2',
            'compare' => '='
        ),
        array(
            'key'     => '_my_custom_key_3',
            'value'   => 'Value I am looking for 3',
            'compare' => '='
        )
    )
);
$meta_query = new WP_Meta_Query( $meta_query_args );

I’m not sure it is possible with :

$user_query = new WP_User_Query($meta_query_args);

I don’t see in the codex if it is possible.

https://codex.wordpress.org/Class_Reference/WP_User_Query

Here is my actual array and it is not working :

Array
(
    [relation] => AND
    [0] => Array
        (
            [relation] => AND
            [0] => Array
                (
                    [fields] => ID
                    [key] => ville
                    [value] => INDRE
                    [compare] => LIKE
                    [type] => CHAR
                )

            [1] => Array
                (
                    [fields] => ID
                    [key] => user_metiers
                    [value] => 297
                    [compare] => LIKE
                    [type] => CHAR
                )

        )

    [1] => Array
        (
            [relation] => OR
            [0] => Array
                (
                    [fields] => ID
                    [key] => last_name
                    [value] => david
                    [compare] => LIKE
                    [type] => CHAR
                )

            [1] => Array
                (
                    [fields] => ID
                    [key] => first_name
                    [value] => david
                    [compare] => LIKE
                    [type] => CHAR
                )

        )

)
1
    $meta_query_args = array(
'meta_query' => array(
    'relation' => 'OR', // Optional, defaults to "AND"
    array(
        'key'     => '_my_custom_key',
        'value'   => 'Value I am looking for',
        'compare' => '='
    ),
    array(
        'relation' => 'AND',
        array(
            'key'     => '_my_custom_key_2',
            'value'   => 'Value I am looking for 2',
            'compare' => '='
        ),
        array(
            'key'     => '_my_custom_key_3',
            'value'   => 'Value I am looking for 3',
            'compare' => '='
        )
    )

);
$meta_query = new WP_User_Query( $meta_query_args );

add 'meta_query' => array(..)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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