Jump to content

Sorting pages by Number of Comments


Ovi
 Share

Recommended Posts

Hi everyone, i have another sorting conundrum. After finding out how to sort pages by a repeater's field value - see http://processwire.com/talk/topic/2884-sorting-based-on-repeater-field-values/ (thanks Nik) now i have another issue.

I guess the title pretty much says it all. I need to sort a collection of pages (products) by most comments (reviews).

According to the method i learned for sorting by a repeater's field, i would need to:

  1. create a hidden field (let's call it "commentsCount")
  2. write a short module that counts all the comments and stores that value in the hidden field as an integer
  3. sort the pages by that field, like: 
$pages->find("template=product, sort=commentsCount");

The problem is that i have nothing to hook that module into. In the case of sorting by a repeater's field it was ok hooking into the Save event because you need to save a page to change a repeater's values. 

But here there's a problem since people can add comments without anybody saving the page and there's no "vent hook available for when a comment is added.

Antti suggested that this is the method that needs to be hook-able but isn't. 

So in conclusion: sorting products by how many reviews they have is a very important functionality of an online store, any suggestions on how to solve this?

Thanks!

EDIT: My only idea so far is hooking the module into something like the save event and then running a cron job that triggers the save event on all pages once per day, thus updating their number of comments count. It's a terrible idea. There has to be a better way to do this...

Link to comment
Share on other sites

Hi Ovi,

the comments get saved, too!

So, where are the comments stored in your hirarchy? I think (in PW-language), the comments are Pages too!

Without knowing your setup and structure, I think it should be possible to hook into page saveready or after page save - of the comments page.

Module:

- check if page is a comment, if no: return

- if yes, get the product-page and count +1 the hidden field

Link to comment
Share on other sites

I think (in PW-language), the comments are Pages too!

Horst,

Actually, from what I can tell from the Blog Profile, they are not PW Pages. :) They are stored in the field (table) "field_comments" (see screenshot). They are associated with their posts (pages) though. I'm sure there is a way to "hook" into them though... O0

post-894-0-92204600-1371484689_thumb.png

Edited by kongondo
  • Like 1
Link to comment
Share on other sites

@Ovi,
 
Excuse my ignorance. I am not sure I get why you need to hook into any method? If a comment is submitted, it is saved to the db (directly if it does not require approval and later if it does require approval by Admin). In that case, why not just count the comments in the db directly? Since comments are related to their pages, you will know how many comments there are per page(?) I'm probably not getting it (I usually don't many times, hehe) and you are far way a better coder than I am, so would request your indulgence to unravel this for me please  :biggrin:
 
Edit:
 
Useful? http://processwire.com/talk/topic/1205-comment-counting-returns-odd-number/
 
OR: Just use PW API in your template file: In my tests, this code sorts my posts (pages) in a list according to number of comments (and echoes the number of comments per post). Since there is a page refresh after each comment submission, the sorting is always up to date...

foreach ($pages->find("template=post, sort=-comments.count") as $reviews ){
	echo "<li>{$reviews->title} - " . count($reviews->comments) . "</li>";
}

Other topics:

http://processwire.com/talk/topic/594-how-can-i-use-pagination-with-comments/

http://processwire.com/talk/topic/357-creating-comments-via-api/

http://processwire.com/talk/topic/1930-comments-question/

http://processwire.com/talk/topic/1624-comments-module-multi-pages-display/

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

Hi Ovi, Hi Kongondo,

 ....

I'm using the comments module http://processwire.com/api/fieldtypes/comments/


Would your suggestion work with that?

haven't worked with it. Don't know it. :mellow:

But Kongondo seams to know it: :)

 Actually, from what I can tell from the Blog Profile, they are not PW Pages. :) They are stored in the field (table) "field_comments" (see screenshot). They are associated with their posts (pages) though. I'm sure there is a way to "hook" into them though... O0 ....

Without knowing anything about Blog Profile, Kongondos suggestion looks very good and promising: ^-^

 ...
OR: Just use PW API in your template file: In my tests, this code sorts my posts (pages) in list according to number of comments (and echoes the number of comments per post). Since there is a page refresh after each comment submission, the sorting is always up to date...

foreach ($pages->find("template=post, sort=-comments.count") as $reviews ){
	echo "<li>{$reviews->title} - " . count($reviews->comments) . "</li>";
}

...

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...