0

I thought the code below would work... but no dice. Something like "AND post_count > 0"

function contributors() { global $wpdb;

$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users WHERE display_name <> 'admin' AND post_count > 0 ORDER BY display_name");

foreach ($authors as $author ) {

echo "<li>";
echo "<a href=\"".get_bloginfo('url')."/author/";
the_author_meta('user_nicename', $author->ID);
echo "/\">";
echo get_avatar($author->ID, 12);
echo "</a>";
echo "<a href=\"".get_bloginfo('url')."/author/";
the_author_meta('user_nicename', $author->ID);
echo "/\">";
the_author_meta('display_name', $author->ID);
echo "</a>";
echo "</li>";
}
}

1 Answer 1

1
$args = array(
    'posts_per_page' => 1,
    'author'         => $author->ID
);
$posts = new WP_Query( $args );
$count = count( $posts );

Put this right after the foreach, $count will now contain either 1 or 0, put it through an if statement to filter the output.

4
  • Hmmm, thanks I tried that and put if ($count = "0") { around the output but nothing changed
    – Josh
    Commented Jan 11, 2012 at 23:31
  • try if( 0 < $count ) {, should do you fine. if( $count = 0 ) { just executes it if it's capable of setting the count to 0. At the very least you'll need ==, but that will only show authors with no posts, so you want $count > 0, but the better way to write this is 0 < $count (precisely because if you try to say 0 = $count, PHP will throw an error, so putting the value first became preferred, regardless of operator)
    – mor7ifer
    Commented Jan 11, 2012 at 23:43
  • Figured it out. I added the following function: $post_count = get_usernumposts($author->ID); And then add an if statement around the output like so: if($post_count > 0){
    – Josh
    Commented Jan 11, 2012 at 23:44
  • Good to hear, don't forget to mark the solution for the next person having this problem :)
    – mor7ifer
    Commented Jan 11, 2012 at 23: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.