0

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

14
  • 1
    Meta keys are not unique, you can have multiple key/value pairs with the same key. Although in this case, if you wanted to know when membership B started, how would you know if they all have the same key? – Tom J Nowell Feb 25 at 13:41
  • @TomJNowell Thanks. I thought something like this should be the result: $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 I´m sure I am missing something, look forward to hearing your thoughts. Thanks – Elv1s Mar 20 at 10:26
  • I don't understand your response, or what the code that would produce that expected result would look like. I don't understand why you can't do what the docs say to do, $results = get_user_meta( $user_id, 'key', false ); foreach ( $results as $result ) { .... – Tom J Nowell Mar 20 at 12:32
  • For example, you say this is what you want th var_dump to look like, but the var_dump or what? You never show what or how you are calling var_dump or where the value comes from – Tom J Nowell Mar 20 at 12:33
  • Also, if you only want the memberships, you should only store the memberships – Tom J Nowell Mar 20 at 12:35
0

You can use insert_user_meta($user_id,'meta_key','meta_value');

when you want to get that keys, you can get like $keys = get_user_meta($user_id,'meta_key');

in $keys variable you get an array of all keys.

1
  • Thanks. I´ve updated / edited my question, I still a bit in the dark :). If you have any further tips, great! – Elv1s Mar 20 at 9:32
0

You've made some fatal misunderstandings that are not supported by the documentation.

  1. Meta keys are not unique
  2. If you store something then retrieve it you will get what you originally stored
  3. It's a good idea to store arrays and objects in meta

Meta keys are not unique

You can have multiple post meta with the same name. When you call add_user_meta you are not updating data, you are adding additional data. Now there are 2 membership post metas. Call it again and there will be 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
}

If you store something then retrieve it you will get what you originally stored

Why would it return just the memberships sub-array? If you store something, you get it back when you retrieve it, not a sub-part of it.

Use get_user_meta to retrieve what you stored.

It's a good idea to store arrays and objects in meta

What if I want all users with a particular membership ID? It can't be done! You can't query inside that value. You can't query inside serialised objects/arrays.

Your membership_id should be... a user meta named membership_id, and each individual membership should be a separate meta.

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.