Try this:
function thebroker_agents() {
global $current_user;
$bmail = $current_user->user_email;
$user_query = new WP_User_Query( array( 'meta_key' => 'broker_email', 'meta_value' => $bmail, 'fields' => 'all' ) );
$users = $user_query->get_results();
if (!empty($users)) {
$results="<ul>";
foreach ($users as $user){
$results="<li>" . $user->display_name . '</li>';
}
$results="</ul>";
} else {
$results="No users found";
}
wp_reset_postdata();
return $results;
}
// Adds the above function as as shortcode
add_shortcode( 'your_agents', 'thebroker_agents' );
You were missing a last bracket. Also I changed the code a bit from echoing, to returning your results. I also reset your post data. Not sure why you didn’t use a while statement in your code, but left that alone.
Since you are calling your function in php, you don’t need a short code. you can call it just like this:
echo thebroker_agents();
However your echo do shortcode will still work as well.