Leave a Reply

2 comments

  1. 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.

  2. 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.