1

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?

1

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.

  • the return - $results line gave me a error so I had to remove the "=" sign. After doing so it still didnt work. @rudtek – Rich Nov 10 '17 at 17:21
  • yep, equal sign was a mistake. What didn't work though? Can you tell me the error? – rudtek Nov 10 '17 at 17:24
  • Without the equal sign it didnt show no error, it didnt show anything. – Rich Nov 10 '17 at 17:30
  • should be working. Try with new updates above. Do you have debug on? Maybe a log issue? – rudtek Nov 10 '17 at 18:39
  • what did you change? It seems to be working now only when viewing the page as a admin. The code is suppose to display when a admin or user with my custom "broker" role views the page. – Rich Nov 10 '17 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.