PHP newbie here, recently decided to have a stab at cobbling together a kooky little 'Random Author Spotlight' feature into my current WordPress theme. I'm almost done, but I've hit something of a snag: I need to somehow make it so that my 'spotlight' will only select users with 1 post or more.

Everyone whom registers on my blog immediately becomes a Subscriber and is able to post news directly on the front page, but most are content with just being regular, post-less members.

I'll only be able to incorporate the 'spotlight' into my theme if I can get it to randomly select a user whom has submitted 1 post or more.

Here's the code so far:

<section id="sidebarAuthorSpotlight">
    <?php $users = get_users(); $id = array_rand( $users, 1 ); $user = $users[$id]; ?>
    Hey, have you met...
    <h3><a class="" href="<?php echo the_permalink(); ?>"><?php echo $user->display_name; ?></a></h3>
    <div><?php echo get_avatar( $user->ID, '60' ); ?></div>
    Mods: <?php echo count_user_posts($user->ID); ?>
    <?php echo $register_date = date("F d, Y", strtotime(get_the_author_meta('user_registered'))); ?>
    <div class="clear"></div>
    <ol class="sidebarPosts">
        <?php $random_author_box = new WP_Query(array('author' => $user->ID,'posts_per_page' => '5')); if ($random_author_box->have_posts() ) : while ($random_author_box->have_posts() ) : $random_author_box->the_post(); ?>
                <a class="sidebarPostTitle" href="<?php echo the_permalink(); ?>"><?php echo the_title(); ?></a>
        <?php endwhile; endif; ?>
    <div class="clear"></div>

Ideally, I'd also love to be able to tweak the registration date to some 'X registered Y days ago', but I'm happy to cross that bridge at a later time. At the moment, I'm in desperate need of getting the above code to filter out users with 0 posts, and only pull users -- or rather a user -- with 1 or more posts.

If anyone could help, I would be so eternally grateful. As someone whom is only now starting to learn the basics of PHP, I can honestly say that I'm in over my head at this point. I look forward to hearing from you guys and thank you each for reading and looking into my plight. Woe is me!

Edit: I am so silly! I should probably explain what the code above does do: the code above does indeed display a random user and provides them with a 'spotlight' area in the sidebar, but it will pull users even with a post count of 0. As explained previous: if it pulled only users with a post count of 1 or more, it would be near-flawless.

1 Answer 1


Your best option is to get a random post author which is a user ID so here is a function to do just that:

function get_random_author_wpa91320(){
    global $wpdb;
    $id = $wpdb->get_var("
        SELECT post_author
        FROM $wpdb->posts
        WHERE post_type = 'post'
        AND post_status = 'publish'
        ORDER BY RAND()
        LIMIT 1
    return $id;

And once you have that function in your theme's functions.php file you can replace this line:

<?php $users = get_users(); $id = array_rand( $users, 1 ); $user = $users[$id]; ?>


<?php $user = get_user_by('id', get_random_author_wpa91320() ); ?>


here is a modified version to get all users from the database who have at least on post and use PHP rand to return a random id

function get_random_author_wpa91320_2(){
    global $wpdb;
    //this will get all users with posts
    $ids = $wpdb->get_col("
        SELECT DISTINCT post_author
        FROM $wpdb->posts
        WHERE post_type = 'post'
        AND post_status = 'publish'
    return $ids[rand(0,count($ids) -1)];

This way is like you count every user once and not based on the post number he has.

  • Wow - thanks, man! Your code appears to work like a charm - cheers! There is one slight side effect, however: the user selected appears to be weighted towards post count. Is there to make it so that all users (with a post count of 1+) have an even chance of being selected, irrespective of post count differences?
    – Steve
    Commented Mar 19, 2013 at 9:12
  • If the user has more posts then he is more likely to be selected, what you can do is change the query to select distinct all post authors and from that return a random ID.
    – Bainternet
    Commented Mar 19, 2013 at 9:46
  • Thank you for getting back to me. Please forgive my ignorance, but how would one go about such a thing? Cheers again for all assistance!
    – Steve
    Commented Mar 19, 2013 at 10:22
  • I updated my answer with an example.
    – Bainternet
    Commented Mar 19, 2013 at 11:42
  • That's absolutely perfect - bravo! I really appreciate it, thank you. Just one last teeny tiny question: would there be a way of echoing the number of subscribers/authors/anybody whom have 1 or more post. To better clarify, if I had 100 registered members, but only 10 whom have published a post, how would I go about grabbing that number, i.e.: <?php echo number_of_users_with_published_posts; ?> It increases all the time, so I can rule out manually updating the number, myself. :p
    – Steve
    Commented Mar 19, 2013 at 15:57

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.