Hey guys,
is there a way to build a voting feature in toolset that can be used in the filter?
What I want to do:
- Each user can vote (once, cookie restriction possible?) for a item
- Theres a field that shows the number of votes
- Just voting wont change the order
- Theres a filter when you can order for the most popular items
Is that something that can be done with toolset? 🙂
Cheers
Jens
Hi, I think it's possible but it would require a bit of custom code. It is not currently possible to sort a View by the number of related posts in a Types post relationship. In order to sort a View of some posts by a number of votes, you would have to include the number of votes in that post type as a custom field. To modify this value on the front-end of the site, you would have to use a Form that edits this post. The Form would simply have a submit button that says something like "Add my Vote". When the Form is submitted, you would use custom code and the Forms API cred_save_data to get the current number of votes and increment it by one, then save that new value. This part requires PHP. Next, you would have to set up some kind of conditional to prevent Users from submitting the Form more than once. One way you could do that is to create a repeating number field on the User profile, where you store the numeric ID of each post the User has already voted for. In the same cred_save_data hook, you could update that custom field value using the ID of the post being edited. The conditional would check to see if the current post ID already exists in the User's custom field. If so, you would not display the edit post Form for voting.
If you want to allow the User to un-vote, that's another level of complexity to consider. Check out these other tickets about Favorites, which includes a similar setup:
https://toolset.com/forums/topic/favorites-view-conditional-button-on-post-add-to-remove-from-favorite-list/
https://toolset.com/forums/topic/favorites-view/
https://toolset.com/forums/topic/favorite-posts-by-logged-in-user-add-to-remove-not-working-properly/
Those examples don't require incrementing or decrementing the vote count, and even without that you can see it's a fairly complex feature to implement. So I would consider other 3rd-party voting plugins as well, and determine which makes the most sense for your project and skill level. Let me know if you have additional questions about this.
Hey Chris,
thanks for your nice answer, even on a sunday! 🙂
To be honest, this extend my skills. Can you recommend any 3rd party plugin that would work with toolset? Do you know any? 🙂
Cheers
Jens
I don't know of any integrations between a voting plugin and Toolset, unfortunately, so I don't have any solid recommendations. I searched the forums for "voting", and another User had some luck with this one: https://wordpress.org/plugins/thumbs-rating/
However, their project was in 2017 so I don't know if that is still relevant:
https://toolset.com/forums/topic/user-upvotes-of-posts/
If a plugin isn't working as expected, I'll be glad to take a closer look.
Thanks Chris! Ill check out your links and see if I can get it done somehow 🙂
Cheers
Jens