WordPress.org

Support

Support » How-To and Troubleshooting » Show only comments by specific registered user

Show only comments by specific registered user

  • jillsays

    @jillsays

    Hi all,
    I’m trying to build some community features into my site, and one of the things I’d like to do on the profile page is show all the last 15 comments by that user, and what thread they were posted in.

    I can’t find codex to help me out with this anywhere. If anyone can help me out, I’d be very grateful.

    Thanks-

Viewing 15 replies - 1 through 15 (of 18 total)
  • Shane G

    @shane-g

    Hi,

    Use this plugin:

    http://wordpress.org/extend/plugins/role-scoper/

    Now, you can create your desired role to your blog and assign that to the user for which you want to allow for comments.

    Thanks,

    Shane G.

    jillsays

    @jillsays

    Hi,

    Thanks for responding. I’m actually just looking for the codex so I can hard-code it into the user’s profile pages.

    I am already using the Alkivia Open Community plugin. As it stands, the codex I’m using only lists the POST TITLE that the user last commented on. I’m trying to figure out how to make it display an excerpt of the comment as well. Here’s the code I’m currently using. Does anyone know what I’d need to add/modify to make it show the particular user’s last 15 comments — and the excerpts of the actual comments?

    Thanks:

    function _recentComments( & $user ) {
    		global $wpdb;
    		$data = array();
    
    		$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' AND user_id = '{$user->ID}' GROUP BY comment_post_ID ORDER BY comment_date_gmt DESC LIMIT 0,5");
    		if ( $comments ) {
    		    $data['title'] = __('Latest coments on:', $this->pid);
    		    foreach ( (array) $comments as $comment ) {
    		        $data['content'][] = '<a href="' . clean_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>';
    		    }
    		}
    
    		$user->aoc_widgets['commentlist'] = $data;
    	}
    jillsays

    @jillsays

    I still haven’t been able to figure this out. Does anyone know the code to display the last, say, 15 comments by a particular registered user?

    Esther

    @webmatter

    Hi, you could try
    get_the_excerpt($comment->comment_post_ID) instead of
    get_the_title($comment->comment_post_ID)
    in your code (or in addtion to get_the_title();) and see if this works .. good luck!

    jillsays

    @jillsays

    Thanks for helping. I tried using get_the_excerpt($comment->comment_post_ID) instead of
    get_the_title($comment->comment_post_ID) and it broke the template.

    I am using this code on another site that DOES display the comment excerpts, but it shows comments by all users, not just one particular user. I just can’t figure out how to blend the two:

    <?php
    global $wpdb;
    $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
    comment_post_ID, comment_author, comment_date_gmt, comment_approved,
    comment_type,comment_author_url,
    SUBSTRING(comment_content,1,75) AS com_excerpt
    FROM $wpdb->comments
    LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
    $wpdb->posts.ID)
    WHERE comment_approved = '1' AND comment_type = '' AND
    post_password = ''
    ORDER BY comment_date_gmt DESC
    LIMIT 5";
    $comments = $wpdb->get_results($sql);
    $output = $pre_HTML;
    $output .= "\n<ul class=sidebar_list2>";
    foreach ($comments as $comment) {
    $output .= "\n<li>".strip_tags($comment->comment_author)
    .":&nbsp" . "<a href=\"" . get_permalink($comment->ID) .
    "#comment-" . $comment->comment_ID . "\" title=\"on " .
    $comment->post_title . "\">" . strip_tags($comment->com_excerpt)
    ."...</a></li>";
    }
    $output .= "\n</ul>";
    $output .= $post_HTML;
    echo $output;?>
    jillsays

    @jillsays

    I clearly don’t know a lot about PHP and wordpress codex. And I’ve been spending multiple hours a day trying to figure this problem out. I’ve been unsuccessful. I must check this thread about 45 times a day to see if anyone has responded. So can’t one of you genius, brilliant, much smarter than I am people take pity on me and help me out? Pretty please?

    jillsays

    @jillsays

    Does anyone have any other ideas? It’s almost 2 weeks later, and I’ve still been unable to modify that code to make it display the actual comment text. Thanks.

    Esther

    @webmatter

    Hi, there is a theme ‘commentpress’ that has this function
    http://www.futureofthebook.org/commentpress/download/
    I just couldn’t figure out how to extract it.
    If you succeed kindly let me know … I am interested in this function, too.

    I still haven’t succeeded – and that theme doesn’t show comments by a particular user, it shows comments by all users.

    Is this just such a difficult issue – or is it that the people who know the answer just can’t be bothered to help?

    Gotta love wordpress “support.”

    Hi. Did you ever get this figured out?

    I found a trac ticket opened – but it isn’t solved yet and it’s for WP 3.0

    Anybody quicker?:)

    I’m also very interested in finding out how to do this…

    It’s stated in this thread by Jane Wells that it should be able to be accomplished on the author page: http://wordpress.org/extend/ideas/topic/user-profile-display-users-posts-and-comments . Or, there may be some confusion about the user page and the author page.

    There is also this plug-in but I’m not sure how it works:
    http://wordpress.org/extend/plugins/get-authors-comments/other_notes/

    and lastly here is the code which provides the listing of posts, not comments (this is shown at http://codex.wordpress.org/Author_Templates ):

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <li>
                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
                <?php the_title(); ?></a>,
                <?php the_time('d M Y'); ?> in <?php the_category('&');?>
            </li>
    
        <?php endwhile; else: ?>
            <p><?php _e('No posts by this author.'); ?></p>
    
        <?php endif; ?>

    Hope we can find a solution…

    Regarding the display of a user’s comments, I ran across http://blogmum.com/2009/06/how-to-make-a-wordpress-profile-page/ with the code:

    <?
    $querystr = "
        SELECT comment_ID, comment_post_ID, post_title
        FROM $wpdb->comments, $wpdb->posts
        WHERE user_id = $thisauthor->ID
        AND comment_post_id = ID
        AND comment_approved = 1
        ORDER BY comment_ID DESC
        LIMIT 10
     ";
    
     $comments_array = $wpdb->get_results($querystr, OBJECT);
    
    if ($comments_array): ?>
    <h2>Recent Comments </h2>
    <ul>
    <? foreach ($comments_array as $comment):
    	setup_postdata($comment);
    	echo "<li><a href='". get_bloginfo('url') ."/?p=".$comment->comment_post_ID."/#comment-". $comment->comment_ID ."'>Comment on ". $comment->post_title ."</a></li>";
    endforeach; ?>
    </ul>
    <? endif; ?>

    but that just causes my author pages to not even display any information at all.

    Anybody know what’s wrong?

    Thanks a bunch!

    @matsim – thanks for sharing the code! Great find!!

    the reason why it doesn’t work is because you are simply dropping the code onto a page, your $thisauthor->ID variable hasn’t been defined so nothing shows. One way to change it is to change the first part like this:

    <?
    if(get_query_var('author_name')) :
    $curauth = get_userdatabylogin(get_query_var('author_name'));
    else :
    $curauth = get_userdata(get_query_var('author'));
    endif;
    $querystr = "
        SELECT comment_ID, comment_post_ID, post_title
        FROM $wpdb->comments, $wpdb->posts
        WHERE user_id = $curauth->ID

    and add it all to your author.php file

    figured out that if you want to show the complete comment change everything to this:

    <?
    if(get_query_var('author_name')) :
    $curauth = get_userdatabylogin(get_query_var('author_name'));
    else :
    $curauth = get_userdata(get_query_var('author'));
    endif;
    $querystr = "
        SELECT comment_ID, comment_post_ID, post_title, comment_content
        FROM $wpdb->comments, $wpdb->posts
        WHERE user_id = $curauth->ID
        AND comment_post_id = ID
        AND comment_approved = 1
        ORDER BY comment_ID DESC
     ";
    
     $comments_array = $wpdb->get_results($querystr, OBJECT);
    
    if ($comments_array): ?>
    <h2>Recent Comments </h2>
    <ul>
    <? foreach ($comments_array as $comment):
    	setup_postdata($comment);
    	echo "<li><a href='". get_bloginfo('url') ."/?p=".$comment->comment_post_ID."'>Comment on ". $comment->post_title. "</a><br />". $comment->comment_content . "</li>";
    endforeach; ?>
    </ul>
    <? endif; ?>
Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Show only comments by specific registered user’ is closed to new replies.