WordPress 通过元数据获取用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6106732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:45:54  来源:igfitidea点击:

WordPress get user by meta data

wordpress

提问by Prashanth

How can I retrieve all users registered in my WordPress blog having a particular meta data?

如何检索在我的 WordPress 博客中注册的所有具有特定元数据的用户?

For example I have made an option to add a custom meta data for every registering users having meta key as parent_id. If I want to list all users having parent_idas 2, then how can I do this?

例如,我选择了为每个具有元密钥的注册用户添加自定义元数据parent_id。如果我想列出所有拥有parent_idas 的用户2,那么我该怎么做?

回答by anroots

Since WP v3.1 it's ridiculously easy to search for a user by his/her meta key.

从 WP v3.1 开始,通过他/她的元键搜索用户非常容易。

Use the function

使用功能

get_users($args)

get_users($args)

(WP Documentation)

WP 文档

The function takes an array of parameters, in your case you need

该函数采用一组参数,在您的情况下,您需要

get_users(array('meta_key' => 'parent_id', 'meta_value' => '42'))

get_users(array('meta_key' => 'parent_id', 'meta_value' => '42'))

回答by OzzyCzech

Simple way how to get one user by his metadata is:

如何通过元数据获取一个用户的简单方法是:

$user = reset(
 get_users(
  array(
   'meta_key' => $meta_key,
   'meta_value' => $meta_value,
   'number' => 1,
   'count_total' => false
  )
 )
);

回答by pingle60

Here is how you can get users based on a custom role and multiple metadata keys,

以下是根据自定义角色和多个元数据键获取用户的方法,

$available_drivers = get_users(
            array(
                'role' => 'driver',
                'meta_query' => array(
                    array(
                        'key' => 'approved',
                        'value' => true,
                        'compare' => '=='
                    ),
                    array(
                        'key' => 'available',
                        'value' => true,
                        'compare' => '=='
                    )
                )
            )
        );

Explaining the above query, I want only those users who I assigned the role of driver, and they are approved and available. The approved and available are custom fields created using ACF as True/False fields.

解释上面的查询,我只想要那些我分配了驱动程序角色的用户,并且他们被批准和可用。已批准和可用是使用 ACF 作为 True/False 字段创建的自定义字段。

If you have additional metadata to test, add another array element to the meta_query array.

如果您有其他元数据要测试,请将另一个数组元素添加到 meta_query 数组。

Meanwhile checkout my open source at github.com/patrickingle

同时在 github.com/patrickingle 查看我的开源

回答by Jem

Here is the codex page from Wordpressdetailing how to use the get_users($arg);function.

这是 Wordpress 的代码页面,详细说明了如何使用该get_users($arg);功能。

It contains examples how how to build custom functions to fetch various parts of user data. You'll have to naturally build and make some of your own changes to get it how you want.

它包含如何构建自定义函数以获取用户数据的各个部分的示例。您必须自然地构建并进行一些自己的更改,以达到您想要的效果。

Additionally here is a linkto a function somebody built that will fetch user data based on roles within wordpress. You can configure it in many different ways with some tweeking, but this will allow you to filter your results in a more powerful manner.

此外,这里有一个指向某人构建的函数的链接,该函数将根据 wordpress 中的角色获取用户数据。您可以通过一些 tweeking 以多种不同方式对其进行配置,但这将使您能够以更强大的方式过滤结果。