I have a function that I use to list users within a specific role, that match a specific meta key value. It works when I use it directly on the page ---
<?php
$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)) {
echo '<ul>';
foreach ($users as $user){
echo '<li>' . $user->display_name . '</li>';
}
echo '</ul>';
} else {
echo 'No users found';
};
?>
But when I try to create as a shortcode --
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)) {
echo '<ul>';
foreach ($users as $user){
echo '<li>' . $user->display_name . '</li>';
}
echo '</ul>';
} else {
echo 'No users found';
}
// Adds the above function as as shortcode
add_shortcode( 'your_agents', 'thebroker_agents' );
And call on the same page like so --
<?php echo do_shortcode('[your_agents]'); ?>
It doesn't do anything except echo out the text - "[your_agents]"
What am I doing wrong with my shortcode function?