Ovi Posted June 17, 2013 Share Posted June 17, 2013 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: create a hidden field (let's call it "commentsCount") write a short module that counts all the comments and stores that value in the hidden field as an integer 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 More sharing options...
horst Posted June 17, 2013 Share Posted June 17, 2013 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 More sharing options...
Ovi Posted June 17, 2013 Author Share Posted June 17, 2013 Thanks for your repply Horst! I'm using the comments module http://processwire.com/api/fieldtypes/comments/ Would your suggestion work with that? Link to comment Share on other sites More sharing options...
kongondo Posted June 17, 2013 Share Posted June 17, 2013 (edited) 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... Edited June 17, 2013 by kongondo 1 Link to comment Share on other sites More sharing options...
kongondo Posted June 17, 2013 Share Posted June 17, 2013 (edited) @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 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 June 17, 2013 by kongondo 2 Link to comment Share on other sites More sharing options...
horst Posted June 17, 2013 Share Posted June 17, 2013 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. 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... .... 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 More sharing options...
Ovi Posted June 18, 2013 Author Share Posted June 18, 2013 Wow, i feel like a dumbass now . Sorry i completely overlooked the .count method available in the PW selectors. Thanks for your help guys! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now