I have a problem which I can't figure out. I have a collection of object which sometimes can be very big. I have to store the entire collection as a serialized transient. The problem is that when the serialized string has more than 60000 characters the insertion in the DB fails.

Why? I checked the wp_options table and the "value" column is a longtext. Which holds 4GB of data, am I right?

  • What's the specific error you're getting? It might not be getting as far as the database, e.g. if the database driver is rejecting the string or maybe PHP's serialization. – Rup Feb 10 '20 at 12:46
  • But that does feel like you ought to break up the transient: are you going to need all 60K's worth of data every time you read it? Or can you divide it up into more specific values, or filter out fields you're serializing that you won't actually need when you're consuming the list, or write the transient as the summary of data you actually need, or something else etc. – Rup Feb 10 '20 at 12:48
  • 1
    Are you using memcached or some other object cache? Those might have their own limits on value size. – Vitauts Stočka Feb 10 '20 at 15:01

I found the problem! Everything was ok with the transient, the problem was that I'm using php's substr() to cut an object property value if exceeds maximum characters. When in that string there was an accented character there was a problem with the encoding. So the serialized collection had an encoding issue at character 60000... I fixed it using substr_mb() and specifying UTF-8 as encoding.

Hard to debug this issue since wpdb doesn't throw any error when there is an encoding problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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