使用WP用户查询构建复杂查询

时间:2017-03-24 作者:Sébastien Gicquel

是否可以使用WP用户查询创建复杂查询?

使用WP Meta Query,我们可以使用嵌套数组进行复杂查询,如:

$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 );
我不确定是否可以:

$user_query = new WP_User_Query($meta_query_args);
我看不到codex 如果可能的话。

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

这是我的实际阵列,它不工作:

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 个回复
最合适的回答,由SO网友:TrubinE 整理而成

    $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 );
添加“meta\\u query”=>数组(..)

相关推荐

警告:CALL_USER_FUNC_ARRAY()要求参数1是有效的回调

我创建了一个新的wordpress,没有主题,只有一个插件:GDPR WP。(我想先在空wordpress上试用这个插件,然后再将它部署到其他网站上)。所以,我第一次尝试创建函数来接受或不接受GoogleAnalytics cookie。我的函数工作正常,但在BO/FO上有一个错误:Warning: call_user_func_array() expects parameter 1 to be a valid callback, function \'cookie_GA\' not found or i