Retrieve WordPress Users by Specified Meta Field

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

I encountered a similar need while working on a client project. An approach that added clarity in my case was using the get_users function, which offers a direct way of querying user meta data. The process is straightforward. For example, I constructed an argument array with the desired meta_key and meta_value. Using get_users, the resulting array of user objects allowed me to loop over them without extra verbosity. I also found that ensuring consistent data types between stored meta values and your query parameters is crucial for avoiding potential mismatches.