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 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.
    • By mattcohen
      Hi, I am wondering if someone can help me out with getting the count of the comments of a page? 
      I am using Ryan's comments module as a reviews system for my site and would like to display the count of the comments per page, as below 
       

       
      The shops part that you see above is something similar to this 
      <?php $set_page = $page->title; $count = $pages->count("template=prices, title=$set_page"); echo $count; ?>  
    • By mtn
      The "Comments" core module has been activated locally with its four associated parts, including an Akismet API key, and has been rendered as per instruction via http://processwire.com/api/fieldtypes/comments/.  Testing the comment form continues to render errors after this manner in the 3.0.96 version.
      Error: Exception: Invalid email address (processwire@localhost:8888) (in ... /wire/core/WireMail.php line 130)  
      After an exhaustive attempt on my part to overcome this error (with limited knowledge), I considered that maybe a corrupt file might be the cause.  However, after updating from 3.0.62 to 3.0.96, the same error persists.  Below is the particular section/line of code mentioned.
      /** * Sanitize an email address or throw WireException if invalid * * @param string $email * @return string * @throws WireException * */ protected function sanitizeEmail($email) { $email = strtolower(trim($email)); $clean = $this->wire('sanitizer')->email($email); if($email != $clean) { $clean = $this->wire('sanitizer')->entities($email); throw new WireException("Invalid email address ($clean)"); //<--- this is the line } return $clean; }  
      Inasmuch as this is the final element that needs to be addressed for this particular site, your assistance in helping me understand the issue and the remedy regarding this matter would be greatly appreciated.  Good day!