0
votes

I would like to have a user meta-data field that is an associative array. Specifically, I would like to store what the user voted for any posts voted for. In other words I would like to have:

for get_user_meta($id,'vote_arr',true) to provide: Array( post_id => 1, 0, -1) where 1 0 or -1 tells me if the user voted up down or neither for that specific post.

However I am having problems retrieving just the $value of the key-value pair using get_user_meta:

I have tried all kinds of combinations:

$vote=get_user_meta(get_current_user_id(),'vote_arr['.$_POST[postId].']',true);
$vote=get_user_meta(get_current_user_id(),'vote_arr['$_POST[postId]']',true);
$vote=get_user_meta(get_current_user_id(),'vote_arr[$_POST[postId]]',true);

none of them provide just the 1,0,-1 value.

Thanks in advance.

1
What does it return? Also it should be 'vote_arr['.$_POST['postId'].']'Rob
Can you share the output of each option that you've tried, and can you give us an example of a database key and value that's stored for a user? This way we can help you narrow down what's going wrong. Passing true at the end of the get_user_meta should cause it to not return an array, but to return the value of that key for that user.rmmoul
nvm, I am just going to do it in two stages: $old_vote_arr=get_user_meta(get_current_user_id(),'vote_arr',true); $old_vote=$old_vote_arr[$_POST['postId']];bob

1 Answers

0
votes

save these in wp_option that will help in storing asso array. use key as "identifier_(userID)". user meta and post meta cannot store array, but wp_options can. use get_option and update_option functions to retrieve data.