如何在前端用Username或First name搜索Wordpress用户?

时间:2014-04-11 10:15:48

标签: wordpress frontend

这是我的代码,但没有得到理想的结果。我只想通过first_name搜索特定用户,其角色是it_guy。有人请帮忙。

$users = new WP_User_Query( 
    array( 

        'role' => 'it_guy',
        'meta_key' => 'first_name',
        'search'  => '*'.esc_attr( $your_search_string ).'*',
        'orderby' => 'first_name',
        'order' => 'ASC',
        'offset' => '',
        'number' => '',

));


foreach($users as $user)
    {
         <?php echo $user->first_name; ?>
    }

1 个答案:

答案 0 :(得分:0)

你应该试试这个,

$users = new WP_User_Query(
        array(
                'role' => 'it_guy',
                'meta_key' => 'first_name',
                'orderby' => 'meta_value',
                'order' => 'ASC',
                'offset' => '',
                'number' => '',
                'meta_query' => array(
                        array(
                                'key'     => 'first_name',
                                'value'   => esc_attr( $your_search_string ),
                                'compare' => 'LIKE'
                        )
                )
        )
);