What is the largest value you can store in a custom field (in the meta data value for a post type)?
-
1I hope I didn't misunderstand what you mean by "largest value"?– Sally CJCommented Aug 10, 2020 at 3:21
-
1That's correct I've updated the question.– 1.21 gigawattsCommented Aug 10, 2020 at 12:12
-
1It's helpful to describe what you're doing - do you mean the biggest number, or perhaps the longest piece of text?– mozbozCommented Aug 10, 2020 at 13:49
1 Answer
The meta value is a LONGTEXT
field which can store about 4 GB (gigabytes) of characters (non-binary), but I can't help you test the limit and note that the number of characters that can be stored will depend on the character encoding.
A quote from MySQL 8.0 Reference Manual:
Note: The four TEXT (data) types are TINYTEXT
, TEXT
, MEDIUMTEXT
, and LONGTEXT
.
VARCHAR
,VARBINARY
, and the BLOB and TEXT types are variable-length types. For each, the storage requirements depend on these factors:
The actual length of the column value
The column's maximum possible length
The character set used for the column, because some character sets contain multibyte characters
For example, a
VARCHAR(255)
column can hold a string with a maximum length of 255 characters. Assuming that the column uses thelatin1
character set (one byte per character), the actual storage required is the length of the string (L
), plus one byte to record the length of the string. For the string'abcd'
,L
is 4 and the storage requirement is five bytes. If the same column is instead declared to use theucs2
double-byte character set, the storage requirement is 10 bytes: The length of'abcd'
is eight bytes and the column requires two bytes to store lengths because the maximum length is greater than 255 (up to 510 bytes).
Another quote from the same manual:
The maximum size of a BLOB or TEXT object is determined by its type, but the largest value you actually can transmit between the client and server is determined by the amount of available memory and the size of the communications buffers. ... You may also want to compare the packet sizes and the size of the data objects you are storing with the storage requirements, see Section 11.7, “Data Type Storage Requirements”
And actually, the question is more about MySQL/database than WordPress-specific API/function, so Stack Overflow is a better place to get further insights on the largest possible column value for LONGTEXT
.
However, just in case it helps, storing a huge number/integer should not be an issue since it's treated as a string in the database, so for example inserting a PHP_INT_MAX
value (which was 9223372036854775807
in my case) worked just fine for me:
add_post_meta( 1, 'foo', PHP_INT_MAX );
$value = get_post_meta( 1, 'foo', true ); // 9223372036854775807
var_dump( PHP_INT_MAX === (int) $value ); // true
References:
TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT maximum storage sizes on Stack Overflow
MySQL 8.0 Reference Manual » 11.7 Data Type Storage Requirements » String Type Storage Requirements
Additional Details
( I initially thought you're asking mainly about the meta key (i.e. name), so I included the following from my original answer. )
The meta key is limited to at most 255 characters (VARCHAR
) and if more than that, (in my case) the meta data is not added/saved, regardless in PHP/WordPress (e.g. using add_post_meta()
) or a direct SQL command (e.g. via phpMyAdmin).
Sample test case (on WordPress 5.4.2 and MariaDB 10.1 with strict mode not enabled):
// Only the *first* metadata gets added.
$id = add_post_meta( 1, wp_generate_password( 255, false ), 'test' ); // meta key = 255 chars
$id2 = add_post_meta( 1, wp_generate_password( 256, false ), 'test' ); // meta key = 256 chars
var_dump( $id, $id2 ); // int(<meta ID>), bool(false)
-
1Based on the info in your link, LONGTEXT is about 4 GB. Commented Aug 10, 2020 at 12:12
-
1Yes, and I've revised the answer. I hope that's more helpful. :)– Sally CJCommented Aug 11, 2020 at 2:20