I am working on a WordPress site and need to obtain a list of users who possess a specific meta field. For example, I have added an extra meta field called custom_parent
for every user during registration. How can I query the database to fetch only those users whose custom_parent
field equals 2? I would appreciate any guidance or code snippet that demonstrates how to execute this task using WordPress functions.
$args = [
'meta_key' => 'custom_parent',
'meta_value' => '2'
];
$userSearch = new WP_User_Query($args);
$resultUsers = $userSearch->get_results();
foreach ($resultUsers as $userDetail) {
echo $userDetail->display_name;
}