Question

I'm trying to get the key values for multiple posts with get_post_meta but am having no luck so far. In short, I have a function that adds 'votes' and 'thevoters' to each post. I want to check to make sure if someone's UserID is in any one of the 'thevoters' fields (spanning across multiple posts) they will not be able to vote again.

The following query gets the value of a specific post. I need to get the values across all posts with that key.

$voters = get_post_meta($id, 'thevoters', true);

My SQL query is:

SELECT * FROM `carcrazy_postmeta` WHERE meta_key='thevoters'

I am using this voting code as a reference - http://bavotasan.com/tutorials/simple-voting-for-wordpress-with-php-and-jquery/

Any ideas?

Was it helpful?

Solution

why not add a filed to user meta once they have voted and just check to see if that specific user can vote?

add to your add vote function this lines:

global $current_user;
get_currentuserinfo();
add_user_meta( $current_user->ID , 'voted', true );

now if a user votes it saves a usermeta filed.

then you can create a simple function to check if a user can vote again:

function has_he_voted($user_id){
    $v = get_user_meta( $user_id , 'voted', true );
    if ($v = true){
        return true;
    }else{
        return false;
    }
}

and to use it and check if the user has voted you just call that function passing a user_id:

if (has_he_voted(12)){
   //can't vote again ,you can only vote once 
   //user has already voted
}else{
   //you can vote
   //user has never voted before
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top