• I’ve got 2 quick related problems that I’d love to get some help on.

    First, I have a PHP function that is supposed to track clicks of a hyperlink and store the values in a post’s custom field. The tracking code is:

    function qwe4_tracker() {
    	global $post;
    			// 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['qwe4'][0]);
    			// increment the count
    			if($views > 0) {
    				update_post_meta($postID, 'qwe4', ($views + 1));
    			} else {
    				add_post_meta($postID, 'qwe4', 1, true);
    			}
    
    }

    So far it works, but the problem is that the function is updating every post’s custom field together instead of individually. How do I get it to update only the post with the link that is being clicked?

    Second:
    How do I properly execute the function only when the hyperlink is clicked?

    Currently I have:
    <a href="<?php qwe4_tracker(); ?>">link</a>
    which runs the script on every page load instead of on the actual click.

    Thanks!

  • The topic ‘Update post meta function’ is closed to new replies.