Home › Forums › General Issues › Request – help with up/downvote feature on relationship field
Hi all,
Apologies if this is posted in the wrong forum, I wasn’t too sure where requests like this should go…
I’m using the relationship field to create a ‘collection’ of posts which I then display in a list view in my theme, I’d like to add the ability for visitors / users to up/downvote posts within the collection (reddit / producthunt style) and for the list to reorder accordingly, is this possible?
It’s also worth mentioning that a post can be contained in multiple collections, so votes would have to be at the level of the collection, rather than assigned to the post.
I know it’s quite a big ask, but if there’s anyone out there who’s able to spare some time to help me get this running I’d appreciate it 🙂
Couple of questions, when you say “collection” of posts, how are you creating this collection? As part of a taxonomy of is just in the relationship field? Do you have multiple post types that you’re using? So basically more information on what you have so far.
I have a couple ideas on how you could go about this and could outline them, but that really depends what you have set up so far.
Hey!
A collection is a post type, i then have the relationship field paired to that post type.
I’m then allowing the relationship field to pull from any other post type (venues and events) to assign posts to the collection
So, this is not a 100% ACF solution, I don’t think that’s possible, and this is just an outline.
To store the vote value use a custom field, you would create a custom field for each collection that an item is in. I would make these hidden meta fields by adding an underscore at the beginning. For example if $collection holds my collection post my meta key would be '_'.$collection
. Then you simply store the current value in this field.
I personally would only store the total from the up votes and down votes, for example each item in the collection would start off with a value of 0. up votes would increase it and down votes would decrease it. I would also use the second option because it will allow me to order the posts easier.
The next step is to create these custom meta fields in the first place. Create a acf/save_post filter as outlined here: http://www.advancedcustomfields.com/resources/acfsave_post/
This filter would run on your collection post type when the collection was saved.
1) Get the posts of the second post type from the relationship field
2) Test to see if the post in the collection has a value set for the custom field for this collection
3) If there is not an existing value then initialize it by created the field and setting it to 0.
Now, when someone votes one way or another you can update this value. I’m assuming you might be using some type of AJAX to do this.
WordPress has recently added functionality that will let you order by these values in a more complex way when doing a query. The following are the meta queries and orderby that you could use.
$meta_key = '_the_collection_field_key';
$clause = $meta_key_clause.'_clause';
$meta_query = array(
'relation' => 'AND',
$clause => array(
'key' => $meta_key,
'compare' => 'EXISTS',
'type' => 'NUMERIC'
)
);
$orderby = array(
$clause => 'ASC'
);
The above meta and orderby values uses the new meta clause feature added in WP v4.2. You can read more about it here: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/
I hope this helps. Like I said it’s only an outline and except for the save post filter, really does not involve ACF. This is definitely a situation where some custom coding is going to need to be done and not really a small project.
The topic ‘Request – help with up/downvote feature on relationship field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.