Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
See the answer i gave to Getting all values for a custom field key –  t31os Mar 1 '11 at 16:07

1 Answer 1

up vote 0 down vote accepted

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
}
share|improve this answer

Your Answer

 
discard

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.