Algorithms, Blockchain and Cloud

How to Create a WordPress Page to List All Comments?


I have created this page and this page and this page to show all the comments page by page. So that you can keep a track of all comments posted sorted by timeline (newest first).

First, you will need to create a page and put the following content (that allows to include a PHP source file in a wordpress page/post):

[ include ]list-of-comments.php[/ include ]

Then create a PHP containing the following source code:

<php
// Author: https://helloacm.com/how-to-create-a-wordpress-page-to-list-all-comments/
  global $wpdb;
  
  $query = "
    select 
        count(1) 
    from 
        `wp_comments`,`wp_posts` 
    where 
        `wp_comments`.`comment_approved` = 1 and 
        `wp_comments`.`comment_post_ID` = `wp_posts`.`ID` and 
        `wp_posts`.`post_status` = 'publish'
  ";
  $total = $wpdb->get_var($query);
  
  $per = 25; // default comments per page
  $page = get_query_var('page');
  if (!$page) {
    $page = 1;
  }
  
  $totalpages = ceil($total / $per);
  if ($page > $totalpages) {
    $page = 1;
  }
  
  $lowerbound = ($page - 1) * $per;
  $upperbound = $lowerbound + $per;
  $lmt=" limit " . $lowerbound . "," . $per;
  
  $query = "select 
    `comment_post_ID`,
    `comment_author`,
    `comment_author_email`,
    `comment_author_url`,
    `comment_date_gmt`,
    `comment_content`,
    `post_title`,
    `post_name`  
   from 
    `wp_comments`,`wp_posts` 
   where 
    `wp_comments`.`comment_approved` = 1 and 
    `wp_comments`.`comment_post_ID` = `wp_posts`.`ID` and 
    `wp_posts`.`post_status` = 'publish' 
   order by 
    `comment_ID` desc $lmt";
  
  $result = $wpdb->get_results($query);
  
  echo "Total <B>$total</B> Comments ($page/$totalpages Pages) - ";  
  if ($page > 1) {
    echo "<a href='?page=".($page-1)."'>Newer Comments</a> - ";
  }
  if ($page < $totalpages) {
    echo "<a href='?page=".($page+1)."'>Older Comments</a>";
  }
  
  echo "<BR/><BR/>";  
  if ($result) {
    $i = $lowerbound + 1;
    foreach ($result as $comment) {
      $created_at = $comment->comment_date_gmt;
      $text = strip_tags($comment->comment_content, "<p><br><div><img><span><pre><video><audio>");
      $author = $comment->comment_author;
      $email = $comment->comment_author_email;
      $url = $comment->comment_author_url;
      $post_id = $comment->comment_post_ID;
      $post_title = $comment->post_title;
      $post_name = $comment->post_name;
      echo "<B>$i</B>. <i>$created_at</i> <a rel=nofollow target=_blank href='$url'>$author</a> Comments on <a href='https://helloacm.com/$post_name/'>$post_title</a>: <BR/><div>$text</div>";
      $i++;
      echo "<BR/><BR/>";              
    }
  }  
  
  if ($page < $totalpages) {
    echo "<a href='?page=".($page+1)."'>Older Comments</a> - ";
  }
  if ($page > 1) {
    echo "<a href='?page=".($page-1)."'>Newer Comments</a>";
  }

Then, you will need a wordpress plugin e.g. Simple PHP Include to include the PHP file in a page. Make sure you clear the cache of the comment page. The default comments shown per page is 25 but you can easily customise this variable.

You also need to check and set the correct preferable URLs for the post, in my case, the URL is in the format of /post_name.

The underlying SQL is just to combine the `wp_comments` and `wp_posts` page so that when listing the comments it only shows the approved comments in the time descending order (the newest comments listed first) with the title of the posts.

–EOF (The Ultimate Computing & Technology Blog) —

666 words
Last Post: How to List the Most-Voted Posts in a Year using SQL?
Next Post: How to Unroll/Flatten Array Recursively using Javascript?

The Permanent URL is: How to Create a WordPress Page to List All Comments? (AMP Version)

Exit mobile version