Adding Multiple Values to a Post Meta Key

I have a question about adding multiple post meta values to a post meta key.

Here is a link to the WordPress add_post_meta(); function in question.

The $unique parameter is a little on the fuzzy side of clarity for me.

Whether or not you want the key to be unique. When set to true, this
will ensure that there is not already a custom field attached to the
post with $meta_key as its key, and, if such a field already exists,
the key will not be added.

If this is set to true, will I be able to add multiple values to a single post meta key?

I am looking to create ONE unique post meta key per post. This key is set as a hidden key. (Prefixed with an underscore – Example: $meta_key = '_task-name';)

Is it possible to add multiple post meta values to this single unique post meta key, in array format?

So when I fetch this post meta key from the post using get_post_meta($post_id, $meta_key); one post might have 3 or 4 tasks names assigned to it.

Would I be better off just using Taxonomies and Terms?

Solutions Collecting From Web of "Adding Multiple Values to a Post Meta Key"

You can insert an associative array into the post_meta_field.

Here is a quick little function to grab the data afterward (did not test):

function grab_task_name_array($post_id) {
    $new_array = array();
    $array = get_post_meta($post_id,'_task-name'); //do not put true as third parameter (this would return string and not array)
    foreach($array[0] as $key => $value)
     $new_array[$key] = $value;

    return $new_array;

If you wanted to store a multidimensional array, some more checks would be needed.

For anyone who might have had the same question…

add_post_meta($post_id, $meta_key, $meta_value, $unique);

The $unique parameter means that there must not already be an existing post meta key for the key / value you are about to add to the post object as post meta.

You can store a single value or an array as post meta. There is no limitation for the type of data stored.

get_post_meta($post_id, $key, $single);

When you fetch the post meta from your post object using get_post_meta(); the $single parameter means you either want to fetch one result as a string, or fetch the entire array of data stored in that particular post meta key.

In my opinion, if you plan on storing multiple pieces of data for a post object, you’re better off using taxonomies and terms. If you’re only storing a quick small array, it’s a simpler solution to just store your data as an array as post meta for your post object.