Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(); ?>
            <li>
                <a class="sidebarPostTitle" href="<?php echo the_permalink(); ?>"><?php echo the_title(); ?></a>
            </li>
        <?php endwhile; endif; ?>
    </ol>
    <div class="clear"></div>
</section>

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.

share|improve this question
add comment

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]; ?>

with

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

Update

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.

share|improve this answer
    
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 Mar 19 '13 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 Mar 19 '13 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 Mar 19 '13 at 10:22
    
I updated my answer with an example. –  Bainternet Mar 19 '13 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 Mar 19 '13 at 15:57
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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