最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

meta query - Fetch all data of Users

programmeradmin2浏览0评论

I am trying to fetch users All information by using below code.

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);

But I am getting only few information like below.

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: ""

I am not getting first_name and last_name of users.

I am trying to fetch users All information by using below code.

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);

But I am getting only few information like below.

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: ""

I am not getting first_name and last_name of users.

Share Improve this question asked Feb 17, 2022 at 13:18 FoysalFoysal 4451 gold badge5 silver badges16 bronze badges 2
  • 1 this is a very strange and incorrect usage of WP_User_Query, what resources told you to do it this way? – Tom J Nowell Commented Feb 17, 2022 at 13:41
  • Thanks @TomJNowell. Actually I am new to meta_query. – Foysal Commented Feb 17, 2022 at 14:07
Add a comment  | 

2 Answers 2

Reset to default 2

Actually since you already have the ID you could just loop through and get the meta.

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

get_user_meta(ID)

There's been a fundamental misunderstanding of how meta_query works. meta_query is meant to be an array, not a new WP_User_Query.

This is what you've done:

get_users( [
    ...args
    'meta_query' => new WP_User_Query( [ ...meta args ] ) // incorrect usage!
] );

This is what you should do instead:

$query = new WP_User_Query( [
    ... args,
    'meta_query' => [ .... meta args ]
] );

Remember, get_users is a wrapper around WP_User_Query, and WP_User_Query is for finding users, not specifying meta parameters.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论