WordPress.org

Forums

Display Author Post Count Outside Loop (5 posts)

  1. Adam Capriola
    Member
    Posted 4 years ago #

    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

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    Did you notice the comment in the code you posted?

    $user_id = $post->post_author;  //change this if not in a std post loop <---
  3. Adam Capriola
    Member
    Posted 4 years ago #

    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.

  4. Adam Capriola
    Member
    Posted 4 years ago #

    blah I figured it out:

    changed: $user_id = $post->post_author;

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

  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    Good job, glad it to hear you got it working... ;)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.