WordPress.org

Support

Support » How-To and Troubleshooting » Get the comments of a post using SQL

Get the comments of a post using SQL

  • Nikolas
    Member

    @kordellas

    Hello, how can I find and display the first comment of the current post I am reading?

    I think a solution will be with SQL.

    Any clues?
    Thank you!

    This is a function that gets 5 recent comments of all posts. I would like to edit this and show the latest of the post I am reading

    function showLatestComments() {
          global $wpdb;
          $sql = "
           SELECT DISTINCT comment_post_ID, comment_author, comment_date_gmt, comment_approved, SUBSTRING(comment_content,1,100) AS com_excerpt
           FROM $wpdb->comments
           WHERE comment_approved = '1'
           ORDER BY comment_date_gmt DESC
           LIMIT 5";
         $comments = $wpdb->get_results($sql);
         $output .= '<h2>latest comments</h2><ul id="comm">';
         foreach ($comments as $comment) {
           $output .= '<li><strong>'. $comment->comment_author . ' said</strong> : "' . strip_tags($comment->com_excerpt). '..."</li>';
           }
         $output .= '</ul>';
         echo $output;
        }//end function
  • The topic ‘Get the comments of a post using SQL’ is closed to new replies.