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?


1 Answer 1


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;
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;
        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
   //you can vote
   //user has never voted before

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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