1

I'm trying to utilize the authors from wordpress to create a spotlight block in the sidebar that selects 1 random author from multiple users and only displays that 1 users information. I want to include the author's display name, a thumbnail, snippet of their bio and a link to the full author page. I haven't found any plugins that support this. I'm not a PHP/MySQL wiz so custom writing a function is a bit outside my abilities however I can read it and follow the code. What I have so far is:

<?php 
    global $wpdb;
    $ids = $wpdb->get_results( "SELECT {$wpdb->usermeta}.user_id from {$wpdb->usermeta} where ({$wpdb->usermeta}.meta_key='wp_capabilities') and ({$wpdb->usermeta}.meta_value >0 )" );
    $user_id = rand(1,count($ids));
    $wpdb->show_errors(); 
?>
<h2><?php get_the_author_meta( 'display_name', $user_id ); ?></h2>
<img class="left" src="" alt="<?php print $selected_user; ?>" />
<p><?php get_the_author_meta( 'user_description', $user_id ); ?> <a href="<?php get_the_author_meta( 'user_url', $user_id ); ?>">read more</a>

This returns an array but I am unsure of what to do to get the output I need. Thanks in advance.

1
  • I opted instead to use a category and use posts instead due to the time crunch the project is under.
    – Keith
    Commented Feb 16, 2012 at 1:45

1 Answer 1

1

You can cut a lot of steps out of this if you use get_users() instead of your custom SQL query. You can then select a random user out of that array using array_rand() (native PHP function, not a wordpress function) and it will return the key you should be using. Here's an example:

$users = get_users( $your_params );
$id = array_rand( $users, 1 );

$user = $users[$id];

This will leave you with $user, which will contain a user object with just about everything you could want on a user...or at least the ability to obtain what you need. get_avatar() will do the thumbnail...assuming you want the default.

2
  • Thanks for your help. Your query results in an error on a current WP install. i get the error "Catchable fatal error: Object of class stdClass could not be converted to string" when I echo $user.
    – Keith
    Commented Feb 16, 2012 at 1:45
  • That's because user is an object, not a string...I made this pretty clear in my answer. You cannot echo objects. Try print_r( $user ); if you want to see what the object contains.
    – mor7ifer
    Commented Feb 16, 2012 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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