Global access to all comments

Recommended Posts

Hi there!

I need to gather a list of all comments, regardless of the parent page of each comment. Is there a global $comments object, which can do this? Something like:

foreach($comments->find('sort=-created') as $comment) {
// show comment

I did not find anything similar in the forums. Can someone help me?

Share this post

Link to post
Share on other sites

I came up with a pretty "close-to-the-metal" solution. Not perfect, but seems to work:

$comments = $database->query('SELECT * FROM field_comments ORDER BY created DESC');
foreach($comments as $c) {
  // $c is an array with raw database values of those comments


Share this post

Link to post
Share on other sites

Not tested, but you could search by template(s) and doing something like:


$replies = $pages->find("template=your_template");
	foreach ($repliess as $reply ) {
		foreach ($reply->comments as $message) {
			# code...



Share this post

Link to post
Share on other sites

@louisstephens Thank you for your thoughts!

Sure, I could use these "nested" queries to gather a "global comments array", which I would to sort by "created" afterwards. But these are too many steps and seems not very performance efficient.

Share this post

Link to post
Share on other sites

this should work :

$field = $fields->get('comments'); 
$comments = $field->type->find($field, "sort=-created");


  • Like 6

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ridgedale
      Please could someone explain how to mark a topic as solved. This does not appear to be covered by the guidelines.
    • By Rudy
      Hi all, Happy New Year!
      Is there a setting to control the amount of Kb of comment text? One of my clients tried to reply to a comment posted and his reply got cut off. I checked it and it's only 2,351 characters. I noticed that maxComment is set to 80K in the module so this did not exceed the max yet.
    • By Batyr
      Hi, I wanted to use Comments module in my project and stumbled upon a problem. Whenever I submit comment with a star rating, it redirects me to unknown page as 
      #CommentForm and my websites preloader is keep turning but nothing is actually happening. Can you please give me a hand on this. What should I do in order to fix this. On my template I added comments field, but submitted comments are not shown. Is there anyway to make comments work on my page?
      Than you in advance

    • By paulb
      I recently started making a blog using Processwire and since i'm completely new, I struggle with a few things.
      I'm trying to make a comment section in my blog post, so I followed the tutorial and pasted the two code lines in my template.
      18 echo $page->comments->render(); 19 echo $page->comments->renderForm();  
      but now I have this error on my page :
      Error: Call to a member function render() on null (line 18 of C:\wamp64\www\processwire\site\assets\cache\FileCompiler\site\templates\Post.php)  
      Am I missing something?
      Thanks for your help.
    • By vadimmil
      I'm testing a news site, build on Processwire. We use standard comments system, provided by PW. I found out, that user can easily crash the whole makeup just with a veeery looong wooord comment. 

       What is the best approach (except pre-moderation of comments) to deal with this problem?