0

What I'm trying to do is show the current logged in user's comment at the top of the list in the comments section in Wordpress on any given post. I don't care if it is just duplicated at the top and still shows in the regular order below as well, I just need it to show at the top of the list so the user can find it easily. I figured out how to single out their comment so I can style it with CSS, but changing where it appears in the list is eluding me. Any help would be incredibly appreciated, thanks in advance guys.

add_filter( 'comment_class', 'comment_class_logged_in_user' );

function comment_class_logged_in_user( $classes ) {
    global $comment;
    if ( $comment->user_id > 0 && is_user_logged_in() ) {
        global $current_user; get_currentuserinfo();
        $logged_in_user = $current_user->ID;
        if( $comment->user_id == $logged_in_user ) $classes[] = 'comment-author-logged-in';
    }
return $classes;
}

This is what I tried for my wp_comment_query, but it doesn't work.

<?php
global $current_user; get_currentuserinfo();
$logged_in_user = $current_user->ID;
$args = array(
   'user_id' => '$logged_in_user',
);

// The Query
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );

// Comment Loop
if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo '<p>' . $comment->comment_content . '</p>';
    }
} else {
    echo 'No comments found.';
}
?>
  • What have you investigated or tried and where exactly do you need help to do it? – cybmeta Mar 10 '16 at 14:09
  • I'm hoping to do it in the comments.php file or else add a function to the functions.php file. After digging though the syntax, the best I could come up with was adding a separate class to the logged in user's comment using this: – Chris Johnson Mar 10 '16 at 18:13
  • I added the existing function to my question above. What I want to do is either order the comments so it appears at the top of the comments or just be able to echo it at the top of the list. I'm flat out stumped as to how to make this happen. – Chris Johnson Mar 10 '16 at 18:21
  • Thanks Michelle, I tried that originally with no luck. I added the comment query code to my original question, if you can spot what I did wrong I'd really appreciate any help you might give. Thanks. – Chris Johnson Mar 10 '16 at 20:27
0

Alright, I figured it out. Thanks for all the direction everyone!

<?php
global $current_user,$post;
$args = array('user_id' => $current_user->ID,'post_id' => $post->ID);

// The Query
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );

// Comment Loop
if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo '<p>' . $comment->comment_content . '</p>';
    }
} else {
    echo 'No comments found.';
}
?>
0

Here's a quick and dirty way to pull comments for the logged-in user:

<?php
$author_id = get_current_user_id();
if( $author_id > 0 ) {
    global $post;
    $args = array(
        'user_id' => $author_id,
        'post_id' => absint( $post->ID),
        'number' => 5,
    );

    $sticky_comments = get_comments( $args );

    if( $sticky_comments ) :
        foreach ( $sticky_comments as $comment ) {
            echo '<p>' . $comment->comment_content . '</p>';
        }
    else :
        echo 'No comments found. ';
    endif;
}
?>

This will run a comment query with an author id of the logged-in user. Be sure to only run the query if an ID is returned from get_current_user_id();.

Then you can just run your normal comment loop after this one in your comments template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.