Fetch all data of Users

时间:2022-02-17 作者:Foysal

我想去拿users 所有信息均使用以下代码。

if($_POST[\'orga_id\']) {
        $users_query = new WP_User_Query(array(
            \'meta_query\' => array(
                \'relation\' => \'AND\',
                array(
                    \'relation\' => \'OR\',
                    array(
                        \'key\'     => \'first_name\',
                        \'value\'   => $_POST[\'search\'],
                        \'compare\' => \'LIKE\'
                    ),
                    array(
                        \'key\'       => \'last_name\',
                        \'value\'     => $_POST[\'search\'],
                        \'compare\'   => \'LIKE\',
                    ),
                ),
                array(
                    \'key\'       => \'organisation\',
                    \'value\'     => $_POST[\'orga_id\'],
                    \'compare\'   => \'LIKE\',
                ),
                array(
                    \'key\'       => \'available\',
                    \'value\'     =>  1,
                    \'compare\'   => \'=\',
                ),          
            )
        ));
    } else {
        $users_query = new WP_User_Query(array(
            \'meta_query\' => array(
                \'relation\' => \'AND\',
                array(
                    \'relation\' => \'OR\',
                    array(
                        \'key\'     => \'first_name\',
                        \'value\'   => $_POST[\'search\'],
                        \'compare\' => \'LIKE\'
                    ),
                    array(
                        \'key\'       => \'last_name\',
                        \'value\'     => $_POST[\'search\'],
                        \'compare\'   => \'LIKE\',
                    ),
                ),
                array(
                    \'key\'       => \'available\',
                    \'value\'     =>  1,
                    \'compare\'   => \'=\',
                ),          
            )
        ));
    }

    //$users_found = $users->get_results();

    $args = array(
        \'fields\' => \'all\',
        \'orderby\'     => array( \'last_name\' => \'asc\', \'first_name\' => \'asc\' ),
        \'meta_query\' => $users_query,
    );

    $users2 = get_users($args);
但我得到的信息很少,像下面这样。

ID: "548"
​​​​
display_name: "AaronKew"
​​​​
user_activation_key: ""
​​​​
user_email: "[email protected]"
​​​​
user_login: "AaronKew"
​​​​
user_nicename: "aaronkew"
​​​​
user_pass: "$P$BauZua136ZxKPY9Nu2FpmZT1LRmOLr1"
​​​​
user_registered: "2022-02-06 03:17:50"
​​​​
user_status: "0"
​​​​
user_url: ""

enter image description here

我不想first_namelast_name 用户数量。

2 个回复
SO网友:Bazdin

事实上,既然你已经有了ID,你就可以通过循环来获取元。

https://developer.wordpress.org/reference/functions/get_user_meta/

get_user_meta(ID)

SO网友:Tom J Nowell

对于如何meta_query 作品meta_query 是一个数组,不是新的WP_User_Query.

这就是您所做的:

get_users( [
    ...args
    \'meta_query\' => new WP_User_Query( [ ...meta args ] ) // incorrect usage!
] );
这是您应该做的:

$query = new WP_User_Query( [
    ... args,
    \'meta_query\' => [ .... meta args ]
] );
回想起get_users 是一个包裹WP_User_Query, 和WP_User_Query 用于查找用户,而不是指定元参数。