Take the 2-minute tour ×

I'm trying to write a function that increments a variable, which will later be stored as post meta for the WordPress post, by clicking the posts title. In other words, I want to a create function that keep track of how many times a post title of a certain post is being clicked.

I'm new to using jQuery so I'm trying to figure out how to get the 2 function in PHP and jQuery to interact. I need to process the data in PHP later, which is why I need to do this.

This is what I have so far (PHP):

function clickScoreMethod($postid){

    //$clickScore = 0;
    $clickScore = get_post_meta($postid, 'redditclickscore', true);
    $clickScore = $clickScore + 0.25; 
    update_post_meta($postid, 'redditclickscore',$clickScore);


$clickScore is the variable I want to increase by 0.25 every time the post title is clicked. I'm using meta to store it easily per post. If it is simpler, I can also increase the score by just incrementing the variable and then dividing it by 4 when I process it in the PHP later.

The jQuery:

$("li.post-item article").each(function(){
    $( ".the-post-container .entry-title" ).click(function() {
        var clickScore = 0.25;

.entry-title is the post title and .post-item-article is basically the blog roll. I have no idea how to get these two to work together so that I increase the number.

share|improve this question

1 Answer 1

Use ajax to do this.

Your jQuery would look something like this:

    var clickScore = 0.25;
      url: "/path/to/php/file",
      type: "POST",
      data: { clickScore: clickScore },
      success: function () {

and you would access the variable and do stuff with it with this (in PHP):

$clickScore = $_POST["clickScore"]

Hope this helps you.

share|improve this answer
Thanks for the reply! Will this continuously increment though? Meaning that if I click the post twice, the total will be 0,5 (0.25+0.25) for that post? If so, how will access the clickScore for any single post? –  arian1123 May 4 at 18:50
@user3601991 It depends on how you use it in your PHP. If you want to store the clickScore permanently, you will need to use MySQL queries. –  natzim May 4 at 18:52

Your Answer


