WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have setup a great voting system on my website, but since I want to sort the top rated posts by week, month and alltime...I now need to able able to keep track of when a vote has been cast. Could anybody help/nudge me in the right direction?

I followed this tutorial:



    jQuery(document).ready(function() {  
    jQuery(".post-like a").click(function(){  
        heart = jQuery(this);  
        // Retrieve post ID from data attribute  
        post_id = heart.data("post_id");  
        // Ajax call  
            type: "post",  
            url: ajax_var.url,  
            data: "action=post-like&nonce="+ajax_var.nonce+"&post_like=&post_id="+post_id,  
            success: function(count){  
                // If vote successful  
                if(count != "already")  
        return false;  

Hooks for functions.php

add_action('wp_ajax_nopriv_post-like', 'post_like');  
add_action('wp_ajax_post-like', 'post_like');  

Post like function:

    function post_like()  
    // Check for nonce security  
    $nonce = $_POST['nonce'];  
    if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )  
        die ( 'Busted!');  
        // Retrieve user IP address  
        $ip = $_SERVER['REMOTE_ADDR'];  
        $post_id = $_POST['post_id'];  
        // Get voters'IPs for the current post  
        $meta_IP = get_post_meta($post_id, "voted_IP");  
        $voted_IP = $meta_IP[0];  
            $voted_IP = array();  
        // Get votes count for the current post  
        $meta_count = get_post_meta($post_id, "votes_count", true);  
        // Use has already voted ?  
            $voted_IP[$ip] = time();  
            // Save IP and increase votes count  
            update_post_meta($post_id, "voted_IP", $voted_IP);  
            update_post_meta($post_id, "votes_count", ++$meta_count);  
            // Display count (ie jQuery return value)  
            echo $meta_count;  
            echo "already";  

Already voted:

    function hasAlreadyVoted($post_id)  
    global $timebeforerevote;  
    // Retrieve post votes IPs  
    $meta_IP = get_post_meta($post_id, "voted_IP");  
    $voted_IP = $meta_IP[0];  
        $voted_IP = array();  
    // Retrieve current user IP  
    $ip = $_SERVER['REMOTE_ADDR'];  
    // If user has already voted  
    if(in_array($ip, array_keys($voted_IP)))  
        $time = $voted_IP[$ip];  
        $now = time();  
        // Compare between current time and vote time  
        if(round(($now - $time) / 60) > $timebeforerevote)  
            return false;  
        return true;  
    return false;  

Is this doable with the code I already have? Any help or advice would be awesome, thanks a lot.

share|improve this question
up vote 2 down vote accepted

The following lines of code both store the IP address and time of vote,

// $ip holds $_SERVER['REMOTE_ADDR']
$voted_IP[$ip] = time();  
// Save IP and increase votes count  
update_post_meta($post_id, "voted_IP", $voted_IP);

The time a vote is cast is already being stored for you in the post_meta table for the given post the person is voting on.

To retrieve the votes and corresponding times for a given post you can do this,

  $mykey_values = get_post_custom_values('voted_IP', $post_id);
  foreach ( $mykey_values as $key => $value ) {
    echo "$key  => $value <br />"; 

That would return a list in a similar format to,

192.166.342.8 => 1349260674
202.646.100.3 => 1349260674
^ IP numbers     ^ unix time stamps
share|improve this answer
Wow, that is amazing man...thanks a lot for posting that up for me. I was previously told that my code didn't allow this (I was trying to sort the posts by week/month/alltime). Can I edit my OP and show you what I had for sorting my posts? Or link it to my previous post? Again, thanks Userabuser – Frank Morrison Oct 3 '12 at 11:28
@FrankMorrison No problem. Well... it sure does track the time. As far as editing your OP, no. Since that is a different question it should be in a different thread UNLESS you modify the title of your question to also include the sorting component then that should be acceptable. BUT if you already have another Question started on WPSE for "sorting" then ignore all of the above and link directly to that question in this comment thread. As far as this question is concerned, if it satisfies your need please mark and accept it as correct to close out this question properly. – userabuser Oct 3 '12 at 23:12
Sure, no problem, I'll close this thread and start another. The post I mentioned is actually on stackoverflow, so might be best to start from scratch over here. I had another go, but not getting anywhere. Do you think im close, or is there a lot to go? If your intrested, here is the other post for now, but I will start a new thread. Anyway, im rambling on here, just want to say thanks for the advice so far, at least I know it does indeed track the time. stackoverflow.com/questions/12619484/… – Frank Morrison Oct 7 '12 at 20:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.