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="https://wordpress.stackexchange.com/questions/91320/<?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="https://wordpress.stackexchange.com/questions/91320/<?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.