Skip to content

Commit bc3fdb4

Browse files
committed
Options/Meta APIs: Document type juggling of meta data.
Document that unserialised data types are stored as strings in the database and returned as such by the meta data functions. For example, setting meta data to the integer value `1` will be returned as `"1"` when subsequently queried via `get_metadata()` and the related functions. Props sukhendu2002, azaozz, jrf, rodrigosprimo. Fixes ticket:61950. git-svn-id: https://develop.svn.wordpress.org/trunk@59657 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ab4b4eb commit bc3fdb4

File tree

6 files changed

+72
-6
lines changed

6 files changed

+72
-6
lines changed

src/wp-includes/comment.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,13 @@ function get_comment_count( $post_id = 0 ) {
432432
*
433433
* @param int $comment_id Comment ID.
434434
* @param string $meta_key Metadata name.
435-
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
435+
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and
436+
* will be returned as the same type when retrieved. Other data types will
437+
* be stored as strings in the database:
438+
* - false is stored and retrieved as an empty string ('')
439+
* - true is stored and retrieved as '1'
440+
* - numbers (both integer and float) are stored and retrieved as strings
441+
* Must be serializable if non-scalar.
436442
* @param bool $unique Optional. Whether the same key should not be added.
437443
* Default false.
438444
* @return int|false Meta ID on success, false on failure.
@@ -481,6 +487,11 @@ function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) {
481487
* False for an invalid `$comment_id` (non-numeric, zero, or negative value).
482488
* An empty array if a valid but non-existing comment ID is passed and `$single` is false.
483489
* An empty string if a valid but non-existing comment ID is passed and `$single` is true.
490+
* Note: Non-serialized values are returned as strings:
491+
* - false values are returned as empty strings ('')
492+
* - true values are returned as '1'
493+
* - numbers are returned as strings
494+
* Arrays and objects retain their original type.
484495
*/
485496
function get_comment_meta( $comment_id, $key = '', $single = false ) {
486497
return get_metadata( 'comment', $comment_id, $key, $single );

src/wp-includes/meta.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
* or any other object type with an associated meta table.
2424
* @param int $object_id ID of the object metadata is for.
2525
* @param string $meta_key Metadata key.
26-
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
26+
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and
27+
* will be returned as the same type when retrieved. Other data types will
28+
* be stored as strings in the database:
29+
* - false is stored and retrieved as an empty string ('')
30+
* - true is stored and retrieved as '1'
31+
* - numbers (both integer and float) are stored and retrieved as strings
32+
* Must be serializable if non-scalar.
2733
* @param bool $unique Optional. Whether the specified metadata key should be unique for the object.
2834
* If true, and the object already has a value for the specified metadata key,
2935
* no change will be made. Default false.
@@ -570,6 +576,11 @@ function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $
570576
* or if `$meta_type` is not specified.
571577
* An empty array if a valid but non-existing object ID is passed and `$single` is false.
572578
* An empty string if a valid but non-existing object ID is passed and `$single` is true.
579+
* Note: Non-serialized values are returned as strings:
580+
* - false values are returned as empty strings ('')
581+
* - true values are returned as '1'
582+
* - numbers (both integer and float) are returned as strings
583+
* Arrays and objects retain their original type.
573584
*/
574585
function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
575586
$value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single );

src/wp-includes/ms-site.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,13 @@ function clean_blog_cache( $blog ) {
10261026
*
10271027
* @param int $site_id Site ID.
10281028
* @param string $meta_key Metadata name.
1029-
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
1029+
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and
1030+
* will be returned as the same type when retrieved. Other data types will
1031+
* be stored as strings in the database:
1032+
* - false is stored and retrieved as an empty string ('')
1033+
* - true is stored and retrieved as '1'
1034+
* - numbers (both integer and float) are stored and retrieved as strings
1035+
* Must be serializable if non-scalar.
10301036
* @param bool $unique Optional. Whether the same key should not be added.
10311037
* Default false.
10321038
* @return int|false Meta ID on success, false on failure.
@@ -1071,6 +1077,11 @@ function delete_site_meta( $site_id, $meta_key, $meta_value = '' ) {
10711077
* False for an invalid `$site_id` (non-numeric, zero, or negative value).
10721078
* An empty array if a valid but non-existing site ID is passed and `$single` is false.
10731079
* An empty string if a valid but non-existing site ID is passed and `$single` is true.
1080+
* Note: Non-serialized values are returned as strings:
1081+
* - false values are returned as empty strings ('')
1082+
* - true values are returned as '1'
1083+
* - numbers (both integer and float) are returned as strings
1084+
* Arrays and objects retain their original type.
10741085
*/
10751086
function get_site_meta( $site_id, $key = '', $single = false ) {
10761087
return get_metadata( 'blog', $site_id, $key, $single );

src/wp-includes/post.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2558,7 +2558,13 @@ function get_posts( $args = null ) {
25582558
*
25592559
* @param int $post_id Post ID.
25602560
* @param string $meta_key Metadata name.
2561-
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
2561+
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and
2562+
* will be returned as the same type when retrieved. Other data types will
2563+
* be stored as strings in the database:
2564+
* - false is stored and retrieved as an empty string ('')
2565+
* - true is stored and retrieved as '1'
2566+
* - numbers (both integer and float) are stored and retrieved as strings
2567+
* Must be serializable if non-scalar.
25622568
* @param bool $unique Optional. Whether the same key should not be added.
25632569
* Default false.
25642570
* @return int|false Meta ID on success, false on failure.
@@ -2615,6 +2621,11 @@ function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
26152621
* False for an invalid `$post_id` (non-numeric, zero, or negative value).
26162622
* An empty array if a valid but non-existing post ID is passed and `$single` is false.
26172623
* An empty string if a valid but non-existing post ID is passed and `$single` is true.
2624+
* Note: Non-serialized values are returned as strings:
2625+
* - false values are returned as empty strings ('')
2626+
* - true values are returned as '1'
2627+
* - numbers (both integer and float) are returned as strings
2628+
* Arrays and objects retain their original type.
26182629
*/
26192630
function get_post_meta( $post_id, $key = '', $single = false ) {
26202631
return get_metadata( 'post', $post_id, $key, $single );

src/wp-includes/taxonomy.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,13 @@ function get_terms( $args = array(), $deprecated = '' ) {
13861386
*
13871387
* @param int $term_id Term ID.
13881388
* @param string $meta_key Metadata name.
1389-
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
1389+
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and
1390+
* will be returned as the same type when retrieved. Other data types will
1391+
* be stored as strings in the database:
1392+
* - false is stored and retrieved as an empty string ('')
1393+
* - true is stored and retrieved as '1'
1394+
* - numbers (both integer and float) are stored and retrieved as strings
1395+
* Must be serializable if non-scalar.
13901396
* @param bool $unique Optional. Whether the same key should not be added.
13911397
* Default false.
13921398
* @return int|false|WP_Error Meta ID on success, false on failure.
@@ -1432,6 +1438,11 @@ function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) {
14321438
* False for an invalid `$term_id` (non-numeric, zero, or negative value).
14331439
* An empty array if a valid but non-existing term ID is passed and `$single` is false.
14341440
* An empty string if a valid but non-existing term ID is passed and `$single` is true.
1441+
* Note: Non-serialized values are returned as strings:
1442+
* - false values are returned as empty strings ('')
1443+
* - true values are returned as '1'
1444+
* - numbers are returned as strings
1445+
* Arrays and objects retain their original type.
14351446
*/
14361447
function get_term_meta( $term_id, $key = '', $single = false ) {
14371448
return get_metadata( 'term', $term_id, $key, $single );

src/wp-includes/user.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,13 @@ function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) {
11511151
*
11521152
* @param int $user_id User ID.
11531153
* @param string $meta_key Metadata name.
1154-
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
1154+
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and
1155+
* will be returned as the same type when retrieved. Other data types will
1156+
* be stored as strings in the database:
1157+
* - false is stored and retrieved as an empty string ('')
1158+
* - true is stored and retrieved as '1'
1159+
* - numbers (both integer and float) are stored and retrieved as strings
1160+
* Must be serializable if non-scalar.
11551161
* @param bool $unique Optional. Whether the same key should not be added.
11561162
* Default false.
11571163
* @return int|false Meta ID on success, false on failure.
@@ -1200,6 +1206,11 @@ function delete_user_meta( $user_id, $meta_key, $meta_value = '' ) {
12001206
* False for an invalid `$user_id` (non-numeric, zero, or negative value).
12011207
* An empty array if a valid but non-existing user ID is passed and `$single` is false.
12021208
* An empty string if a valid but non-existing user ID is passed and `$single` is true.
1209+
* Note: Non-serialized values are returned as strings:
1210+
* - false values are returned as empty strings ('')
1211+
* - true values are returned as '1'
1212+
* - numbers (both integer and float) are returned as strings
1213+
* Arrays and objects retain their original type.
12031214
*/
12041215
function get_user_meta( $user_id, $key = '', $single = false ) {
12051216
return get_metadata( 'user', $user_id, $key, $single );

0 commit comments

Comments
 (0)