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

How to pull all the contributer users records and order by Designation (which is users meta data)?

programmeradmin3浏览0评论

I want to show all the contributors by the Designation. Following code is only pull the records and order by ID But I need to data order by Designation:

function contributors() {
global $wpdb;
 
$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users WHERE display_name <> 'admin' ORDER BY id DESC");
 
foreach ($authors as $author ) {
 
    echo get_wp_user_avatar($author->ID);

    the_author_meta('designation', $author->ID);

    the_author_meta('fullname', $author->ID);

    the_author_meta('company', $author->ID);

    the_author_meta('address', $author->ID);

    the_author_meta('phone', $author->ID);

    }
}

Could you please help me out?

Thanks.

I want to show all the contributors by the Designation. Following code is only pull the records and order by ID But I need to data order by Designation:

function contributors() {
global $wpdb;
 
$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users WHERE display_name <> 'admin' ORDER BY id DESC");
 
foreach ($authors as $author ) {
 
    echo get_wp_user_avatar($author->ID);

    the_author_meta('designation', $author->ID);

    the_author_meta('fullname', $author->ID);

    the_author_meta('company', $author->ID);

    the_author_meta('address', $author->ID);

    the_author_meta('phone', $author->ID);

    }
}

Could you please help me out?

Thanks.

Share Improve this question asked Feb 11, 2022 at 17:40 Manoj MauryaManoj Maurya 213 bronze badges 2
  • you shouldn't be using a raw SQL query to do this, WP_User_Query can do what you're doing, and it can filter by user meta. Also use get_user_meta not the_author_meta – Tom J Nowell Commented Feb 11, 2022 at 21:02
  • Thanks @TomJNowell for the hint. I went through the WP_User_Query document and got the solution. – Manoj Maurya Commented Feb 23, 2022 at 10:49
Add a comment  | 

1 Answer 1

Reset to default 2

I got the answer after went through WP_User_Query documentation. Following is the final query:

$args = [
    'role__not_in' => 'Administrator',
    'meta_key' => 'designation',
    'meta_query '=> [
        'meta_key' => 'designation'
    ],
    'orderby' => array(
        'meta_value'=>'ASC'
    )
];
$my_user_query = new WP_User_Query( $args );
发布评论

评论列表(0)

  1. 暂无评论