Skip to:
Content
Pages
Categories
Search
Top
Bottom

How do you show a member's total post count?

  • Avatar of snark
    snark
    Participant

    @snark

    I would like to show the total post count for each user, both in their member profile (below the username), and below their name whenever it appears in posts, comments, Forum Topics and Replies, activity streams, etc. Something like this:

    John Doe

    279 posts

    This would be a way for everyone to easily identify the most frequent posters to the site. Anybody know how to do this?

Viewing 20 replies - 1 through 20 (of 20 total)
  • Avatar of Xevo
    Xevo
    Participant

    @xevo

    Forum posts or blog posts?

    Blog posts is gonna be more difficult since users can have more than one blog.

    Avatar of snark
    snark
    Participant

    @snark

    No, NOT blog posts. I am running single user WP, and all the blog posts are done by only 3 people, so I rather have the post count be for everything BUT blog posts, but if it has to include blog posts, that’s fine. As long as it includes all Forum Topics and Replies.

    i would also like to know how to do this

    Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    See if this does what you want. Put the following function in [your-theme]/functions.php or [plugin-dir]/bp-custom.php:

    function get_activity_count_by_user( $user_id ) {
    $args = array(
    'per_page' => 10000,
    'show_hidden' => true,
    'user_id' => $user_id
    );

    if ( bp_has_activities( $args ) ) {
    global $activities_template;
    $count = $activities_template->total_activity_count;
    } else {
    $count = 0;
    }

    return $count;
    }

    Then call the function somewhere in a template, something like this:

    <?php echo get_activity_count_by_user( $user_id ) ?>

    making sure that $user_id is populated with the user_id of the person you are querying about.

    You could filter this in various ways. To get just one kind of content, or content from just one component of the site, try some of the filters here: http://codex.buddypress.org/developer-docs/custom-buddypress-loops/the-activity-stream-loop/. To exclude things (like anything having to do with blogs) is a bit tricker; after calling the $activities_template global, loop through each member of $activities_template->activities and check to see whether it’s the kind of thing you want. If not, reduce $activities_template->total_activity_count by one.

    Avatar of snark
    snark
    Participant

    @snark

    Thanks @boonebgorges — I got it partially working, but then got stuck. I put the function as is above in my functions.php file, and added this to member-header.php:

    <p><?php echo get_activity_count_by_user( bp_current_user_id() ) ?> Forum posts</p>

    This returned ALL posts for a user, including blog posts. Obviously, based on the text I added above, I only want to include Forum posts (should include both Topics and Replies). I looked at the page you suggested — http://codex.buddypress.org/developer-docs/custom-buddypress-loops/the-activity-stream-loop/ — but I couldn’t figure out how or where to implement the filter: what syntax, and where does it go, in the function or in the call-out?

    Also: Using “bp_current_user_id() ” allows the post count to display on the user’s profile page, but how do I display the post count on a different page, such as /single/forum/topic.php, the Forum topics display page, where “bp_current_user_id()” is no longer applicable? On that page I’d like to put a user’s post count in parenthesis next to the user, like this: “John Doe (54 posts)”. That way when you scanned the list of Replies to a Topic, you would quickly see if a user was a long-term and/or hardcore user or a noob based on the number of posts they have contributed to the site, and I’m hoping that little ego boost will help nudge people into posting more frequently. Does that make sense?

    Thanks again for all your help.

    Avatar of 3sixty
    3sixty
    Participant

    @3sixty

    snark, try this and let me know if it works? it is designed to count all forum topics started by the user. Because the bp_has_activities() only accepts one action type (I think), you would have to create 2 functions like this and add them together – one for new_forum_topics and one for new_forum_posts.

    function get_new_forum_topic_count_by_user( $user_id ) {
    $args = array(
    'per_page' => 10000,
    'show_hidden' => true,
    'user_id' => $user_id,
    'action' => 'new_forum_topic'
    );

    if ( bp_has_activities( $args ) ) {
    global $activities_template;
    $count = $activities_template->total_activity_count;
    } else {
    $count = 0;
    }

    return $count;
    }

    Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    @3sixty‘s solution is a good one.

    As for the question of how to get the user id, it really depends on exactly where you’re trying to pull it up. It sounds like in this case you want the user id for the last poster in a topic, so you should go to the template that controls the forum loop (bp-themes/bp-default/forums/forums-loop.php and see what’s being used to pull up the last poster’s name. In this case it’s bp_the_topic_last_poster_name. So go looking for that function. Usually if a function is referenced in a template file, it’s in the corresponding component -templatetags.php file; in this case, it’s buddypress/bp-forums/bp-forums-templatetags.php. If you look at the definition for bp_the_topic_last_poster_name, you can see that it’s getting its information from the global $forum_template that happens in the forum loop. Use print_r to print that global to your screen, and look for the ID that you’ll need.

    Avatar of snark
    snark
    Participant

    @snark

    Thanks @boonebgorges and @3sixty — I went with @3sixty‘s solution and decided to make a copy of the function for the post count and then call them out as separate “Forum Topics” and “Forum Replies” counts, rather than totaled together, in the yellow activity bar on the member profile page like this:

    <span class="activity">
    <?php bp_last_activity( bp_displayed_user_id() ) ?>
    | Forum Topics: <?php echo get_new_forum_topic_count_by_user( bp_current_user_id() ) ?>
    | Forum Replies: <?php echo get_new_forum_post_count_by_user( bp_current_user_id() ) ?>
    </span>

    This works well, as you can see on the member pages at http://www.wordlab.com/. I decided to forgo adding the post count to the topic list anyway, so I’m all set.

    The only bit of information I would have liked to have added to the member profile pages is a count of blog posts, which I figured out easily enough, but to only have that count display on the member profiles of those members who have permission to post to the blog, which on this site is only 3 out of over 200 members. So for the three members who have blog posts, there would be an added count, like this, “Blog posts: 46″, but for all other users instead of saying “Blog posts: 0″, I tried to craft a conditional tag but I couldn’t get it to work right — the count just kept showing up on all user profile pages. Any ideas?

    Avatar of 3sixty
    3sixty
    Participant

    @3sixty

    Why wouldn’t this work?:

    $blog_post_count = result of whatever function you created to count blog posts. Then:

    if ($blog_post_count > 0) echo “Blog posts: ” . $blog_post_count;

    So if it’s 0, nothing gets displayed.

    could you use this to show activity update posts count?? if so, how?

    Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    thanks :)

    Avatar of snark
    snark
    Participant

    @snark

    Thanks @3sixty — after much head-wringing and a little tweak, I got your code to work perfectly:

    <?php if (get_usernumposts( bp_current_user_id() ) > 0) echo " | Blog posts: " . get_usernumposts( bp_current_user_id() ); ?>

    In the process I learned that “get_usernumposts()” has been deprecated in WordPress 3.0, so when that comes out I assume I’ll have to change this to work with the replacement function, “count_user_posts()”

    Avatar of Nm
    nm
    Participant

    @nahummadrid

    is there a way to put a time parameter on a member post and/or comment count.

    Hello, I want to display the total forum post count on the topic.php right next to the avatar.

    I tried both @boonebgorges code, and the other, but all the counts some up as “2″ when there clearly more than 2.

    Can somebody help me?

    should I start a new topic?

    Avatar of James
    James
    Participant

    @janismo

    @boonebgorges @modemlooper @snark

    hi guys, found this old topic and have to conclude that during last year no one has appeared with other function to count user’s activities. Unfortunately, as modemlooper mentioned, issue with incorrect count is still in there.
    could you please tell if there were some changes in BP to get this number, or maybe you have some working function?

    thank you.

    Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

    http://codex.wordpress.org/Function_Reference/count_user_posts

    You would need to get the displayed bp user id for $userid

    Avatar of James
    James
    Participant

    @janismo

    thanks for reply,
    taking into account name of the function and result that I get, this function is meant for blog posts (I understand that this is clearly stated in the name of this topic too), but what I need is number of all activities, as it was discussed in the beginning of the topic.
    @boonebgorges wrote nice function for this, but with an issue, which @modemlooper has described previously.

    you’ll need to perform a sql count query on the activity table for a given user_id

Viewing 20 replies - 1 through 20 (of 20 total)

You must be logged in to reply to this topic.