0

In my site i need to store 200 extra data for each user. Now i know about the user meta option. But is it a good way to save 200extra field for each user?

Or should i create an extra table in the database to do the work.

3
  • I would probably create a new table. It also depends on how often you will need to access the data.
    – Brooke.
    Commented Mar 22, 2013 at 4:52
  • do you need to query users on the data, i.e. get users based on the value of a specific piece of extra data? any data that doesn't need to used in a query can be stored as array within a single field
    – Milo
    Commented Mar 22, 2013 at 5:00
  • I only need to get data of a user. No need to do any query using that data. How an array can be saved in field? Can you give me some example url? Commented Mar 22, 2013 at 5:35

1 Answer 1

0

You can store multiple pieces of data within a single field by passing an array rather than a string:

$user_data = array(
    'favorite_color' => 'blue',
    'favorite_animal' => 'cat',
    'favorite_city' => 'paris' 
);
update_user_meta( $user_id, 'user_data', $user_data );

WordPress will serialize this data for you on save, and unserialize back into an array when it is retrieved:

$user_data = get_user_meta( $user_id, 'user_data', true );
echo $user_data['favorite_color'];

The caveat is that it will actually result in a slightly larger amount of data, but it will decrease the number of rows consumed by the data.

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.