gerritvanaaken

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:

<?php

$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 quickjeff
      Hi Guys,
      I am running a page template that contains a filter form that allows users to display results based on number of  comments. 
      Here is what I am thinking about doing: 
      -add new field called total_comments 
      -somehow populate this field every time a new comment is added or removed by admin with count($page->comments)
      -use form filter to display results that the selector form has found.
      Example: $selector = “total_comments>=$q”; 
       
      My question to everyone: Is this a good approach? If so how do I get another field to update based on the comment module adding or removing a comment? 
    • By quickjeff
      Hey Guys, 
      I am using the https://processwire.com/api/fieldtypes/comments/ to  allow users to comment and rate. Whats the best approach to get an average of all the star ratings for the page?
      Any direction is greatly appreciated!  
    • By gonzz
      I'm doing an artist site and they want to be able to upload a large .tiff to their portfolio. I need to keep to a copy of the original file in the server for the art distributers, and of course serve jpg in the web front end.
      I had the idea of extending the file or image upload input so that if the image is tiff, keep it, and also convert it to jpg, so I could access through something like $image->originalUrl or $image->size(100, 100)->url 
      I was wondering if you thought this a good idea, or if i'm over-engineering something that could be done simply. Also, I've never extended an input, so any examples are welcome.
      Thanks!
    • By chcs
      In the page reference field, one can create new pages if they enable that option. However, the "new" page only gets the title field filled out.
      Q: Is there a way to expand on this so a user can fill in more fields from the new page? Either in-line or via modal?
       
    • By chcs
      I have a page with a good amount of fields on it, I have many other pages in the same template that have no issue, but one particular page just doesn't save changes to any of the fields. 
      I installed the clone page module, cloned the page, and everything cloned fine... but still when i try to make changes to the clone nothing saves either (just like the original page). 
      I expect some kind of notification to show up saying it saved, or not, but i get nothing as if the page reloaded fresh. The failure is silent, and there's nothing in the logs to direct me.
      This is a real head scratcher and I'm not sure how I should go about troubleshooting it further. Can someone help?
      Thank you kindly.