I have a custom field called count
. I am trying to increase and save its value to a post when a vote button is clicked for the image of the post. However, it doesn't update the value of custom field nor does it update on the page...nothing happens
<?php
// Query the custom post type to display
$args = array('post_type' => 'books');
$query = new WP_Query( $args );
while ( $query->have_posts() ) :
$query->the_post();
if ( has_post_thumbnail() ):
$oldcount=get_field('count');
$newcount=$oldcount+1;
?>
<div><?php the_post_thumbnail('thumbnail'); ?><button onclick="myvote($post_id,$newcount)">Vote</button><?php echo " ".$oldcount;?></div>
<?php endif; endwhile; ?>
<script language="Java Script" type="text/javascript">
function myvote($pid,$tempcount) {
$.ajax({
type: "POST",
url: "<?php bloginfo('template_directory'); ?>/voteajax.php",
data: {action:'call_this',pid:$pid,tempcount:$tempcount},
cache: false,
success: function(html){
$("body").append(html);
}
});
};
</script>
voteajax.php
<?php
if($_POST['action'] == 'call_this') {
update_field('field_5501', $_POST['tempcount'],$_POST['pid'];
}
?>