As @s_ha_dum has stated, your syntax mixes JS and PHP; and Ajax wil be needed. I’ll try to point you in the correct direction.
This should be your javascript:
jQuery(document).ready(function($) {
$('button#rsvp_button').click(function qwe4_tracker(){
var data = { action: 'my_action' };
$.post(ajaxurl, data, function(response) {
//alert('Got this from the server: ' + response); // Uncomment to use for testing
});
});
});
NOTES:
-
We use
jQuery(document).ready(function($){
to begin our JS. This is to prevent clashes when using the global$
jQuery variable. -
The var
data
contains theaction
; which must be used in the PHPadd_action
described below.
And this should be your PHP function to handle the AJAX:
add_action('wp_ajax_my_action', 'my_action_callback');
function my_action_callback() {
global $post; // You still may need to pass the POST ID here from the JS ajax.
// get each post by ID
$postID = $post->ID;
// get the post's custom fields
$custom = get_post_custom($postID);
// find the view count field
$views = intval($custom['number_attending'][0]);
// increment the count
if($views > 0) {
update_post_meta($postID, 'number_attending', ($views + 1));
} else {
add_post_meta($postID, 'number_attending', 1, true);
}
die(); // this is required to return a proper result
}
The PHP function will handle the updating of the number in the post meta.. and the JS tells the PHP (via ajax) to update when the button is clicked.
NOTE:
You may still have an issue getting the proper Post ID. If so, then you are going to need to declare this in a global JS variable via your PHP code.
Good Luck!