custom field – How to save multiple values with same meta_key, each value linked to another tag id

Question

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

0
Elv1s 5 months 2021-02-25T06:08:33-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse