WordPress.org

Support

Support » Plugins and Hacks » Hacks » Display Author Post Count Outside Loop

Display Author Post Count Outside Loop

  • I found this topic very useful and it gave this bit of code:

    <?php
    global $wpdb;
    $user_id = $post->post_author;  //change this if not in a std post loop
    $where = 'WHERE comment_approved = 1 AND user_id = ' . $user_id ;
    $comment_count = $wpdb->get_var(
        "SELECT COUNT( * ) AS total
    		FROM {$wpdb->comments}
    		{$where}
    	");
    $user = get_userdata($user_id);
    $post_count = get_usernumposts($user->ID);
    echo '<p>User ' . $user->display_name . ' post count is ' . $post_count .', comment count is ' . $comment_count . '</p>';
    ?>

    I’m just not sure how to change it to work outside the loop. I’m not sure if something from Author Templates needs to be used or what.

    Thanks in advance for any help… I’m sure it is a “duh” change to the code.

    -Adam

Viewing 4 replies - 1 through 4 (of 4 total)
  • Did you notice the comment in the code you posted?

    $user_id = $post->post_author;  //change this if not in a std post loop <---

    Yes I noticed it. I don’t know what to change it to. 🙁

    I tried adding this before that code:

    <?php
    if(isset($_GET['author_name'])) :
        $curauth = get_user_by('slug', $_GET['author_name']);
    else :
        $curauth = get_userdata($_GET['author']);
    endif;
    ?>

    Then changed that line to:

    $user_id = $curauth->ID;

    But it didn’t work.

    blah I figured it out:

    changed: $user_id = $post->post_author;

    to: $user_id = get_the_author_meta( 'ID' );

    Good job, glad it to hear you got it working… 😉

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display Author Post Count Outside Loop’ is closed to new replies.