Let´s say a user signs up to multiple memberships, each on a different date.
User signs up to:
- Membership A on 05.03.2021
- Membership B on 17.05.2021
- Membership C on 29.07.2021
I would like the dates for that user to be saved with the same meta_key.
Each saved value/date should be linked to the specific membership, so that I can run a check and get the date when the user signed up for a specific membership.
I have the following so far:
$parameters = array(
\'member_id\' => $user_id,
\'membership\' => array (
array (
\'tags\' => $tags,
\'datetime\' => time(),
)
)
);
add_user_meta( $user_id, \'membership\', $parameters );
I would want to have the result for a user be something like:
membership:
[0]
Tag: Membership A
Date: 05.03.2021
[1]
Tag: Membership B
Date: 17.05.2021
[2]
Tag: Membership C
Date: 29.07.2021
In the var_dump
it would look something like, where the number of sub-arrays for "membership" will be different per user, depending on the number of memberships they have assigned to them.
array(2) {
["member_id"]=>
string(2) "12"
["memberships"]=>
array(3) {
[0]=>
array(2) {
["membership"]=>
string(5) "membership A"
["datetime"]=>
int(1616239233)
}
[1]=>
array(2) {
["membership"]=>
string(5) "membership B "
["datetime"]=>
int(1616239233)
}
[2]=>
array(2) {
["membership"]=>
string(5) "membership C"
["datetime"]=>
int(1616239233)
}
}
}
I´m sure I am missing something, look forward to hearing your thoughts. Thanks
SO网友:Tom J Nowell
您犯了一些文档不支持的致命误解。
元键不是唯一的,如果您存储了某个内容,然后检索它,您将得到原来存储的内容,最好将数组和对象存储在元中,元键不是唯一的,您可以有多个同名的post Meta。当你打电话的时候add_user_meta
您不是在更新数据,而是在添加其他数据。现在有2个membership
后图元。再打一次,就会有3个。
add_user_meta( $user_id, \'example\', \'test\' );
add_user_meta( $user_id, \'example\', \'test\' );
add_user_meta( $user_id, \'example\', \'test\' );
$examples = get_user_meta( $user_id, \'example\', false );
foreach ( $examples as $example ) {
// there are 3 examples
}
如果您存储了某个内容,然后将其检索,那么您将获得原来存储的内容
为什么它只返回成员身份子数组?如果你储存了一些东西,你在取回它的时候就会取回它,而不是它的一个子部分。
使用get_user_meta
检索存储的内容。
将数组和对象存储在meta中是一个好主意,如果我希望所有用户都具有特定的成员身份ID,该怎么办?这不可能!不能在该值内查询。无法在序列化对象/数组内查询。
你的membership_id
应该是。。。名为的用户元membership_id
, 每个成员都应该是一个单独的元。