By John

2012-04-17 19:20:31 8 Comments

I tried to store an array containing about 50000 keys and respective values to the user meta using update_user_meta. Since, this was only a test, I ran a for loop to generate the array.

$test = array ();
for ($i = 0; $i <= 50000; $i++) {
    $test[$i] = $i;
update_user_meta($user_id, 'test', $test);

I ended up getting the error WordPress database error: [MySQL server has gone away]. Eventually, I was able to get it working with an array size of 48000 keys. Since update_user_meta converts arrays to serialized data before storing, is there a limit to the serialized data that user meta or post metas can handle? Or does this happen due to some other issue?

I tried the timeout fix suggested here. But I am not sure if it is supposed to work for WP 3.3.1. or whether if at all it is solution to my problem.

Please note I am using a local dev install using MAMP.


@Tom J Nowell 2012-04-17 19:29:03

According to the DB schemea, the meta_value column is of type longtext, and that has a maximum size of 4GB, however you're unlikely to reach that.

Eitherway storing large quantities of data in a single field is bad both from a practical point of view, a performance point of view, and data storage.

I advise instead you use a custom taxonomy for this kind of data ( yes taxonomies aren't just for post objects )

@John 2012-04-17 19:44:16

Thx for the reply. The data I need to store to user meta would be done for each user registered on the site. In future the user base could climb to 100K users or more. Wouldn't saving the data as custom taxonomy for each user bloat the database?

@Tom J Nowell 2012-04-17 20:51:09

No more than saving it as user meta, BUT, if you ever needed t do queries on that data it would be much much easier to do it using the taxonomy than to search through user meta

@John 2012-04-19 16:57:22

After lot of deliberation I have finally understood the benefits of using user taxonomies for this need. I ended up creating a user taxonomy with the same name as the username of a User A. And all other users whose ID I earlier used to add to meta data of User A, I now add to the user taxonomy that was created as above. Please advise if this approach is correct? Secondly, I see wp_user_query class for querying users. Not sure how to make it work with user taxonomy. Could you please update your answer with an example on how to run queries to fetch users using user taxonomy?

@Tom J Nowell 2012-04-19 17:19:28

A taxonomy is used to classify objects, or more specifically object IDs. Most people use this as in post IDs, and thus assume it means only post IDs. Please refer here:…

@Tom J Nowell 2012-04-19 17:20:36

There's also custom post types, where the term slug is the user login/user ID

@John 2012-04-19 17:45:34

Sorry, I did not understand your comment. Let me rephrase my question. I have gone through Justin's article on custom user taxonomy and successfully created them as well. My question now is how do I fetch a list of user objects added to a custom user taxonomy using wp_query or wp_user_query?. I have not found any references to the same in Justin's article.

@Tom J Nowell 2012-04-19 17:51:56

e.g. $users = get_objects_in_term( $practice->term_id, 'practice' );

@John 2012-04-19 18:22:24

With posts and post taxonomies you could run a query like $query = new WP_Query( array('books' => 'latin') );. Can't we do something similar with wp_user_query or may be custom $wpdb query and passing custom user taxonomy as the parameter? Rather than loading a variable with array of user object id's. Something on the lines of $user_query = new WP_User_Query( array('profession' => 'developer') );

@Tom J Nowell 2012-04-19 19:28:58

Not that I'm aware of, but the lack of support is pointed out in tadlocks article too

@John 2012-04-19 19:54:44

Thx a million for your help. I'll probably ask new question to understand what we could do with custom user taxonomies and queries. Once again, Thx.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Can I set user meta for theoretical user 0?

  • 2019-08-04 01:13:47
  • Matthew Brown aka Lord Matt
  • 105 View
  • 1 Score
  • 1 Answer
  • Tags:   user-meta

1 Answered Questions

[SOLVED] Wordpress REST API and User meta data

1 Answered Questions

[SOLVED] Add custom user meta data

  • 2019-05-17 15:18:56
  • Subham
  • 955 View
  • 0 Score
  • 1 Answer
  • Tags:   users user-meta

1 Answered Questions

get_users when from meta key that has serialized values

  • 2017-08-31 11:09:24
  • Dgeorgex000000
  • 280 View
  • 0 Score
  • 1 Answer
  • Tags:   php user-meta array

1 Answered Questions

Fetch User Meta Data

2 Answered Questions

[SOLVED] How to access User meta data within a plugin

  • 2013-09-07 12:42:51
  • Vijay Rajasekaran
  • 762 View
  • 0 Score
  • 2 Answer
  • Tags:   user-meta

1 Answered Questions

[SOLVED] How to update serialized data in the user meta data

1 Answered Questions

Sponsored Content