Title: Update post meta function
Last modified: August 19, 2016

---

# Update post meta function

 *  [echstudios](https://wordpress.org/support/users/echstudios/)
 * (@echstudios)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/update-post-meta-function/)
 * 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.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [function](https://wordpress.org/support/topic-tag/function/)
 * [hyperlink](https://wordpress.org/support/topic-tag/hyperlink/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [echstudios](https://wordpress.org/support/users/echstudios/)
 * Last activity: [16 years, 10 months ago](https://wordpress.org/support/topic/update-post-meta-function/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
