• Resolved Alkorr

    (@alkorr)


    Hi, I would like to show the 5 last comments from the author of the post, on the post page. I tried many codes but I can’t get the comments…

    Here is the code so far:

    <?php
    	$number=5;
    	$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' and comment_author_email='$curauth->user_email' ORDER BY comment_date_gmt DESC LIMIT $number");
    ?>
    
    <?php
    	if ( $comments ) : foreach ( (array) $comments as $comment) :
    	echo  '<a href="'. get_comment_link($comment->comment_ID) . '">' . get_the_title($comment->comment_post_ID) . '') . '';
    	endforeach; else: ?>
    <?php _e('No comments from the author.'); ?>
    <?php endif; ?>

    Thanks for your help! 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Alkorr

    (@alkorr)

    Hi, I tried to modify my code but it’s getting worse… 🙁

    Any help is welcome, thank you! 🙂

    Thread Starter Alkorr

    (@alkorr)

    Can someone please tell me if the code is right or totally going in the opposite way of what I’m trying to achieve? Because I’m quite lost there…

    Thanks for your help!

    Thread Starter Alkorr

    (@alkorr)

    Still no idea?

    Happy New Year! 😉

    This should work for posts by a registered user:

    <?php
        $number=5;
        $authorid = $post->post_author;
        $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' and user_id='$authorid' ORDER BY comment_date_gmt DESC LIMIT $number");
    ?>
    
    <?php
    if ( $comments ) : foreach ( $comments as $comment) :
        echo  '<li><a href="'. get_comment_link($comment->comment_ID) . '">' . get_the_title($comment->comment_post_ID) . '</a></li>';
        endforeach;
    else: ?>
       <?php _e('No comments from the author.'); ?>
    <?php endif; ?>

    I’m not sure if you can do anything for posts by unregistered users since I think the post_author is zero, and would match all comments by unregistered users.

    Thread Starter Alkorr

    (@alkorr)

    That works perfectly! Thank you so much for your precious help vtxyzzy!! 🙂

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

The topic ‘Get last comments from author’ is closed to new replies.