I'm a novice so please bear with me. Thank you.
- I created 2 custom post types. brands and models.
- I created relationship custom fields using acf under brands
- The relationship field pulls the posts from models
- Inside the models I created custom fields for vote counter. I used the custom field for the voting so we could see the vote count on each model in the backend as well as the frontend.
So this is the sequence
- brand post > Pulls model post (using relationship field) > model post pulls vote field
Now, this is where I'm having trouble.
I created a single post template for brand. The template displays all related data for a single brand and displayed the models related to the brand using foreach, this includes the vote field.
I created a script that will execute the ajax function on click to update the vote field. I used get_post_meta to get the car model vote field and update_post_meta to update vote field.
On click, the vote field number on the frontend went up but it wasn't saved. Everything is reset to 0 when the browser is refreshed. It's not saving on the backend either.
If you could enlighten me, I would really appreciate it. Thank you.
$data = [];
$id = (int)$_POST['post_id'];
$vote = get_post_meta($id, 'vote', true);
if( !$vote ) $vote = 0;
$vote = (int)$vote;
$vote++;
update_post_meta( $id, 'vote', $vote );
$data['vote'] = $vote;
die( json_encode($data) );